🌵 搭建第一个python flask 应用。
搭建第一个 Python Flask 应用非常简单。Flask 是一个轻量级的 Web 框架,适合快速开发小型 Web 应用。以下是搭建第一个 Flask 应用的步骤:
1. 安装 Flask
首先,你需要确保已经安装了 Python。然后,使用 pip
安装 Flask:
pip install Flask
2. 创建项目结构
创建一个项目文件夹,并在其中创建一个 Python 文件(例如 app.py
):
mkdir my_flask_app
cd my_flask_app
touch app.py
3. 编写 Flask 应用
在 app.py
文件中编写以下代码:
from flask import Flask
# 创建一个 Flask 应用实例
app = Flask(__name__)
# 定义一个路由和视图函数
@app.route('/')
def hello_world():
return 'Hello, World!'
# 运行应用
if __name__ == '__main__':
app.run(debug=True)
4. 运行 Flask 应用
在终端中运行以下命令来启动 Flask 应用:
python app.py
默认情况下,Flask 应用会在 http://127.0.0.1:5000/
上运行。打开浏览器并访问这个地址,你应该会看到页面上显示 "Hello, World!"。
5. 调试模式
在上面的代码中,app.run(debug=True)
启用了调试模式。调试模式会在代码发生变化时自动重新加载应用,并且在出现错误时提供详细的错误信息。
6. 添加更多路由
你可以通过添加更多的路由来扩展你的应用。例如:
@app.route('/about')
def about():
return 'This is the about page.'
访问 http://127.0.0.1:5000/about
将会显示 "This is the about page."。
7. 使用模板
Flask 支持使用 Jinja2 模板引擎来渲染 HTML 页面。首先,创建一个 templates
文件夹,并在其中创建一个 index.html
文件:
mkdir templates
touch templates/index.html
在 index.html
文件中编写以下内容:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>My Flask App</title>
</head>
<body>
<h1>{{ message }}</h1>
</body>
</html>
然后,修改 app.py
文件以使用模板:
from flask import Flask, render_template
app = Flask(__name__)
@app.route('/')
def hello_world():
return render_template('index.html', message='Hello, World!')
if __name__ == '__main__':
app.run(debug=True)
现在,访问 http://127.0.0.1:5000/
将会显示一个带有 "Hello, World!" 标题的 HTML 页面。
8. 静态文件
Flask 还支持静态文件(如 CSS、JavaScript 和图片)。你可以在项目文件夹中创建一个 static
文件夹来存放这些文件。例如:
mkdir static
然后在 static
文件夹中创建一个 style.css
文件:
body {
background-color: lightblue;
}
在 index.html
文件中引用这个 CSS 文件:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>My Flask App</title>
<link rel="stylesheet" href="{{ url_for('static', filename='style.css') }}">
</head>
<body>
<h1>{{ message }}</h1>
</body>
</html>
现在,页面的背景颜色将会变成浅蓝色。
9. 部署应用
当你准备好将应用部署到生产环境时,可以使用 gunicorn
或 uWSGI
等 WSGI 服务器来运行 Flask 应用。例如,使用 gunicorn
:
pip install gunicorn
gunicorn -w 4 app:app
这将启动一个带有 4 个工作进程的 Flask 应用。
总结
通过以上步骤,你已经成功搭建了一个简单的 Flask 应用。Flask 非常灵活,适合从小型项目到大型应用的开发。你可以根据需要继续扩展和定制你的应用。