{% raw %}
Python 和 Flask Web 应用程序(入门教程)
使用 Flask 创建的 Python 应用
在本教程中,您将学习如何使用 Python 构建网络应用。 我们将使用称为 Flask 的微型框架。
为什么是 Flask?
易于使用。
内置开发服务器和调试器
集成单元测试支持
RESTful 请求分派
使用 Jinja2 模板
支持安全 cookie(客户端会话)
100%符合 WSGI 1.0
基于 Unicode
大量记录
创建 URL 路由
URL 路由使 Web 应用程序中的 URL 易于记住。 现在,我们将创建一些 URL 路由:
/hello
/members/
/members/name/
复制下面的代码,并将其另存为app.py
from flask import Flask
app = Flask(__name__)
@app.route("/")
def index():
return "Index!"
@app.route("/hello")
def hello():
return "Hello World!"
@app.route("/members")
def members():
return "Members"
@app.route("/members/<string:name>/")
def getMember(name):
return name</string:name>
if __name__ == "__main__":
app.run()
使用以下命令重新启动应用程序:
$ python hello.py
* Running on http://localhost:5000/
在浏览器中尝试网址:
python-flask-webapp
Flask 页面模板
我们将使用称为模板的技术将代码和用户界面分开。 我们创建名为/templates/
的目录并创建模板:
<h1>Hello {{name}}</h1>
Python Flask 应用具有新的 URL 路由。 我们将默认端口更改为 80,即默认 HTTP 端口:
from flask import Flask, flash, redirect, render_template, request, session, abort
app = Flask(__name__)
@app.route("/")
def index():
return "Flask App!"
@app.route("/hello/<string:name>/")
def hello(name):
return render_template(
'test.html',name=name)</string:name>
if __name__ == "__main__":
app.run(host='0.0.0.0', port=80)
You can then open : http://127.0.0.1/hello/Jackson/
给模板添加样式
您是否需要一些外观更好的模板? 我们修改文件:
{% extends "layout.html" %};
{% block body %};
<div class="block1">
<h1>Hello {{name}}!</h1>
<h2>Here is an interesting quote for you:</h2>
"The limits of my language are the limits of my mind. All I know is what I have words for."
<img src="http://www.naturalprogramming.cimg/smilingpython.gif">
</div>
{% endblock %};
然后,我们创建layout.html
来定义页面的外观。 (您可能想要拆分样式表和layout.html
文件)。将此复制为layout.html
<title>Website</title>
<style>
@import url(http://fonts.googleapis.com/css?family=Amatic+SC:700);</p>
<p>body{<br />
text-align: center;<br />
}<br />
h1{<br />
font-family: 'Amatic SC', cursive;<br />
font-weight: normal;<br />
color: #8ac640;<br />
font-size: 2.5em;<br />
}</p>
</style>{% block body %};{% endblock %};
重新启动应用程序并打开 URL:http://127.0.0.1/hello/Jackson/,您可以选择杰克逊以外的任何其他名字。
python webapp flask
传递变量
让我们显示随机引文,而不是总是相同的引文。我们将需要同时传递name
变量和quote
变量。要将多个变量传递给函数,我们只需执行以下操作:
return render_template(
'test.html',**locals())
我们新的test.html
模板如下所示:
{% extends "layout.html" %};
{% block body %};
<div class="block1">
<h1>Hello {{name}}!</h1>
<h2>Here is an interesting quote for you:</h2>
{{quote}}
<img src="http://www.naturalprogramming.cimg/smilingpython.gif">
</div>
{% endblock %};
我们将需要选择一个随机引文。 为此,我们使用以下代码:
quotes = [ "'If people do not believe that mathematics is simple, it is only because they do not realize how complicated life is.' -- John Louis von Neumann ",
"'Computer science is no more about computers than astronomy is about telescopes' -- Edsger Dijkstra ",
"'To understand recursion you must first understand recursion..' -- Unknown",
"'You look at things that are and ask, why? I dream of things that never were and ask, why not?' -- Unknown",
"'Mathematics is the key and door to the sciences.' -- Galileo Galilei",
"'Not everyone will understand your journey. Thats fine. Its not their journey to make sense of. Its yours.' -- Unknown" ]
randomNumber = randint(0,len(quotes)-1)
quote = quotes[randomNumber]
您看到的第一件事是我们定义了一个包含多个引文的数组。 这些可以作为quote[0]
,quote[1]
,quote[2]
等访问。函数randint()
返回一个介于 0 和引文总数之间的随机数,因为我们从零开始计数,所以减去了一个。最后,我们将quote
变量设置为计算机选择的引文。将以下代码复制到app.py
:
from flask import Flask, flash, redirect, render_template, request, session, abort
from random import randint
app = Flask(__name__)
@app.route("/")
def index():
return "Flask App!"
#@app.route("/hello/<string:name>")
@app.route("/hello/<string:name>/")
def hello(name):
# return name
quotes = [ "'If people do not believe that mathematics is simple, it is only because they do not realize how complicated life is.' -- John Louis von Neumann ",
"'Computer science is no more about computers than astronomy is about telescopes' -- Edsger Dijkstra ",
"'To understand recursion you must first understand recursion..' -- Unknown",
"'You look at things that are and ask, why? I dream of things that never were and ask, why not?' -- Unknown",
"'Mathematics is the key and door to the sciences.' -- Galileo Galilei",
"'Not everyone will understand your journey. Thats fine. Its not their journey to make sense of. Its yours.' -- Unknown" ]
randomNumber = randint(0,len(quotes)-1)
quote = quotes[randomNumber] </string:name></string:name>
return render_template(
'test.html',**locals())
if __name__ == "__main__":
app.run(host='0.0.0.0', port=80)
重新启动应用程序时,它将随机返回这些引文之一。
python flask webap
接下来是什么?
您可以将您的站点链接到数据库系统,例如 MySQL,MariaDb 或 SQLite。 您可以在中找到 SQLite 教程。 享受创建您的应用程序的乐趣!
{% endraw %}