Hello World:您的第一个 Flask 应用
编写 app.py 并创建 Flask 应用对象
Hello World:您的第一个 Flask 应用 是 CoddyKit 上的免费 Flask Academy 课时。 这是第 3 节课,共 4 节。 你可以在下方免费阅读本课时的完整内容 — 然后在浏览器中使用内置代码编辑器和全天候 AI 导师进行实践。 这是 Flask Academy 学习路径的一部分,你的进度在网页和 CoddyKit 应用中同步。 Flask Academy 课程共包含 4 节课。
本课时的部分内容尚未翻译,以英文显示。
Time to Build
You will now write your first Flask app and return a real response. It is just a few lines, and you already have everything installed. 🙌
Create app.py
Make a file named app.py in your project folder. This single file will hold your tiny first application.
Import Flask
Start by importing the Flask class. Everything you build hangs off the app object you are about to create.
from flask import FlaskCreate the App Object
Instantiate Flask and pass __name__ so it knows where your app lives and can locate templates and static files.
app = Flask(__name__)What __name__ Does
The __name__ value tells Flask the import name of your module, which it uses to resolve resource paths correctly.
Add a Route
The @app.route decorator ties a URL path to the function right below it. Here the homepage path is just a slash.
@app.route('/')
def home():
return 'Hello, World!'The View Function
The function under a route is a view. Whatever string it returns becomes the body the browser displays.
Return a Response
Returning plain text is the simplest response. Flask wraps your string in a proper HTTP response automatically. 📨
The Main Guard
An if __name__ block lets you run the file directly. Inside it you call app.run to start the development server.
if __name__ == '__main__':
app.run()Run Your App
Launch it from the terminal. Flask prints a local address, usually on port 5000, where your app is now listening.
python app.pySee It in the Browser
Open http://127.0.0.1:5000 and your greeting appears. You just served your first web page with Python.
Quick Check
Quick check on wiring up your first app.
Recap
You imported Flask, built the app object, added a route and view, and ran it. Hello World is live in your browser. 🌍
常见问题解答
「Hello World:您的第一个 Flask 应用」课时是免费的吗?
是的 — 「Hello World:您的第一个 Flask 应用」的完整文本可在网页上免费阅读。要进行交互式练习(内置代码编辑器和全天候 AI 导师)并解锁 Flask Academy 课程的其余内容,请升级到 CoddyKit PRO。 Flask Academy 课程共包含 4 节课。
「Hello World:您的第一个 Flask 应用」这节课中我会学到什么?
编写 app.py 并创建 Flask 应用对象 你通过在浏览器中直接运行的动手代码来练习 Flask Academy,全天候 AI 导师会在你学习这节课的过程中回答你的问题。
学习 Flask Academy 需要有经验吗?
无需任何先前经验。CoddyKit 上的 Flask Academy 课程适合初学者到高级学习者,你可以从这里开始或从头开始,按照自己的节奏学习。 这是第 3 节课,共 4 节。
「Hello World:您的第一个 Flask 应用」课时需要多长时间?
大多数 CoddyKit 课程大约需要 5–10 分钟。每节课都很精短且互动,所以你能稳步进步,并在网页和应用中从离开的地方继续。
我能在这节 Flask Academy 课中编写并运行代码吗?
能。每节 Flask Academy 课都包含内置代码编辑器,你可以在浏览器中直接编写并运行真实代码,并获得即时 AI 反馈 — 无需本地设置。
此课程中的所有课时
- Flask 是什么以及它为何存在
- 设置虚拟环境并安装 Flask
- Hello World:您的第一个 Flask 应用
- 以调试模式运行开发服务器