python写网页前端页面源代码_Python编程flask使用网页模版的方法

在flask中可以像go和angular那样使用网页模版(template),可以将HTML网页显示进行模版化,通过参数传递与网页进行数据交互。

概要信息

事前准备:flask

liumiaocn:flask liumiao$ which flask

/usr/local/bin/flask

liumiaocn:flask liumiao$ flask –version

Flask 1.0.2

Python 2.7.10 (default, Jul 15 2017, 17:16:57)

[GCC 4.2.1 Compatible Apple LLVM 9.0.0 (clang-900.0.31)]

liumiaocn:flask liumiao$

代码示例:嵌入式的HTML模版

像Angular一样,我们可以在flask中写前端的网页,python代码中混杂着HTML代码,在这里将前面的HelloWorld示例进行简单的改,将显示的Hello World加上的设置。

代码示例

liumiaocn:flask liumiao$ cat flask_1.py

!/usr/bin/python

from flask import Flask

app = Flask(name)

@app.route(“/”)

def hello():

return “

Hello World!

if name == “main“:

app.debug=True

app.run(host=’0.0.0.0′,port=7000)

liumiaocn:flask liumiao$

执行&确认

在HelloWorld示例中我们提到有两种方式启动flask的微服务进程,这里再添加一种,添加#!/usr/bin/python之后,同时对此文件添加可执行权限比如755,就行了使用.启动

liumiaocn:flask liumiao$ chmod 755 flask_1.py

liumiaocn:flask liumiao$ ./flask_1.py

  • Serving Flask app “flask_1” (lazy loading)

  • Environment: production

WARNING: Do not use the development server in a production environment.

Use a production WSGI server instead.

  • Debug mode: on

  • Running on http://0.0.0.0:7000/ (Press CTRL+C to quit)

  • Restarting with stat

  • Debugger is active!

  • Debugger PIN: 131-533-062

通过curl进行结果确认:

liumiaocn:flask liumiao$ curl http://localhost:7000

Hello World!

liumiaocn:flask liumiao$

网页确认

代码示例

上面的示例过于简单,写一个简单的完美的网页来确认一下

liumiaocn:flask liumiao$ cat flask_1.py

!/usr/bin/python

from flask import Flask

app = Flask(name)

@app.route(“/”)

def hello():

return ‘ \

\

\

Hello \

\

Hello World!

\

if name == “main“:

app.debug=True

app.run(host=’0.0.0.0′,port=7000)

liumiaocn:flask liumiao$

执行&确认

通过curl可以确认网页范围信息

liumiaocn:flask liumiao$ ./flask_1.py

  • Serving Flask app “flask_1” (lazy loading)

  • Environment: production

WARNING: Do not use the development server in a production environment.

Use a production WSGI server instead.

  • Debug mode: on

  • Running on http://0.0.0.0:7000/ (Press CTRL+C to quit)

  • Restarting with stat

  • Debugger is active!

  • Debugger PIN: 131-533-062

也可以通过浏览器来确认title和网页显示

网页模版

嵌在python的代码中非常的麻烦,转义的连接,以及源码的查看都非常不方便。flask提供了Jinja2的模版渲染,只需要引入render_template就行了使用。

import render_template

为了使用这个功能,首先需要在程序中做如下import

from flask import render_template

准备网页信息

比如将上文中嵌入的HTML网页独立成index.html,具体信息如下:

liumiaocn:flask liumiao$ ls templates/

index.html

liumiaocn:flask liumiao$ cat templates/index.html

Hello Template

Hello World!

liumiaocn:flask liumiao$

注重事项:flask会在当前目录的templates下搜索对应的模版文件,所以需要创建templates文件夹,然后将模版html文件放入其中。

网页调用

在网页上只需要调用render_template就行了实现url与对应模版的关联,

render_template(“index.html”)

具体代码

liumiaocn:flask liumiao$ cat flask_2.py

!/usr/bin/python

from flask import Flask

from flask import render_template

app = Flask(name)

@app.route(“/”)

def hello():

return render_template(“index.html”)

if name == “main“:

app.debug=True

app.run(host=’0.0.0.0′,port=7000)

liumiaocn:flask liumiao$

执行&确认

liumiaocn:flask liumiao$ python flask_2.py

  • Serving Flask app “flask_2” (lazy loading)

  • Environment: production

WARNING: Do not use the development server in a production environment.

Use a production WSGI server instead.

  • Debug mode: on

  • Running on http://0.0.0.0:7000/ (Press CTRL+C to quit)

  • Restarting with stat

  • Debugger is active!

  • Debugger PIN: 131-533-062

使用curl可以看到具体的html代码,而且读起来方便多了

liumiaocn:~ liumiao$ curl http://localhost:7000

Hello Template

Hello World!

liumiaocn:~ liumiao$

也可以通过浏览器确认并查看源码

小结

使用render_template,flask也可以像angular一样非常方便的创建用于展示的模版视图,我们已经说过render_template是使用Jinja2的模版,在下一篇文章中将继续简介template的数据交互和控制方式。

总结

以上就是这篇文章的全部内容了,希望本文的内容对大家的学习或者工作具有一定的参考学习网上卖,谢谢大家对乐购源码的支持。如果你想理解更多相关内容请查看下面相关链接

Original: https://blog.csdn.net/weixin_36049771/article/details/112925388
Author: 兮扬
Title: python写网页前端页面源代码_Python编程flask使用网页模版的方法

原创文章受到原创版权保护。转载请注明出处:https://www.johngo689.com/749513/

转载文章受原作者版权保护。转载请注明原作者出处!

(0)

大家都在看

亲爱的 Coder【最近整理,可免费获取】👉 最新必读书单  | 👏 面试题下载  | 🌎 免费的AI知识星球