FLASK云部署【完整项目】
admin
2023-09-07 16:10:00
0

上期给大家介绍了 【Windows】基于FLASK部署图像识别模型 ,这次给大家介绍下flask的基本框架以及云端部署。

1 flask框架

flask是一种基于python,并且依赖于Jinja2模板引擎(提供网页解析)和 Werkzeug WSGI服务(python web应用与web 服务之间的接口)的一种微型框架。其中:

  • Jinja2模板引擎:通俗来讲就是服务器接收到用户请求之后,将数据传入HTML文件中后,经过模板引擎的渲染将其呈现在网页中响应给用户。
  • Werkzeug WSGI:python web 应用程序是一个被调用的对象,它无法直接与web 服务器直接建立联系,所以WSGI的功能就是提供程序与服务之间的通信。它规定了一个app接口,server会传递给 web 应用所有的请求信息以及响应之后需要调用的函数。

flask主要由三部分组成,static文件夹(存储静态文件),template文件夹(存储网页),应用文件app.py

-demo
-static
-css
-index.css
...
-js
-index.js
...
-img
...
-templates
index.html
...
index.py
...


1.1 static文件夹

动态的 web 应用也需要静态文件,一般是 CSSJavaScript 文件。

使用特定的 static 端点可以生成相应的 URL。

url_for('static', filename='css/style.css')

这个静态文件在文件系统中的位置是 static/css/style.css

1.2 模板文件

在python中需要自己写 html,并使用 render_template() 方法渲染模板。

注意,Flask 会在 templates 文件夹内寻找模板。

from flask import render_template

@app.route('/hello/')
@app.route('/hello/')
def hello(name=None):
return render_template('hello.html', name=name)

模板示例:


Hello from Flask
{% if name %}

Hello {{ name }}!


{% else %}

Hello, World!


{% endif %}

1.3 app.py

  • __name__:是一个适用于大多数情况的快捷方式。有了这个参数, Flask 才能知道在哪里可以找到模板和静态文件等东西
from flask import Flask

app = Flask(__name__)
  • 使用 route()装饰器来把函数绑定到 URL
# / 表示网站的根路径,默认ip和端口号:127.0.0.1:5000
@app.route('/')
def index():
return 'Index Page'

@app.route('/hello')
def hello():
return 'Hello, World'

除了上述静态url,还可使用动态url,其通过将传递给函数的参数作为url的一部分,采用的形式,同时可以选择性的加上一个转换器,类似

转换器有以下几种类型:



最终形式如下:

from markupsafe import escape

@app.route('/user/')
def show_user_profile(username):
# show the user profile for that user
return f'User {escape(username)}'

@app.route('/post/')
def show_post(post_id):
# show the post with the given id, the id is an integer
return f'Post {post_id}'

@app.route('/path/')
def show_subpath(subpath):
# show the subpath after /path/
return f'Subpath {escape(subpath)}'

flask建表时,初次运行,需要加上db.create_all()

if __name__ == "__main__":
db.create_all()
app.run(debug=True)

1.4 模板继承

在目录里可以看到有三个 .html ,其中update.htmlindex.html继承了base.html,这个就是模板继承。









{% block head %}{% endblock %}


{% block body %}{% endblock %}


代码中,{% block %} 标记了2个可以被子模板填充的块block 标记告诉模板引擎这是一个可以被子模板重载的部分。

模板继承采用如下语句:

{% extends 'base.html' %}

{% extends %} 告诉模板引擎这个模板“扩展”了另一个模板, 当模板系统评估这个模板时会先找到父模板。这个扩展标记必须是模板中的第一个标记。 如果要使用父模板中的块内容,使用 {{ super() }}

1.5 flask+tornado+nginx

在window上部署flask,一般采用flask+tornado+nginx的形式。

打开nginx官网下载:http://nginx.org/en/download.html

下载完成后,解压缩,不要直接双击nginx.exe,而是运行cmd,打开到nginx.exe所在的目录:

#启动nginx服务
start nginx
# 关闭nginx
nginx -s stop
nginx -s quit

nginx文件夹中的conf/nginx.conf添加如下内容(http中的内容):

http {
server {
listen 80;//监听那个端口
server_name 127.0.0.1;//这里是你服务器的域名或者IP

location / {
proxy_pass http://127.0.0.1:5000;//转发到那个端口
}
}
}

app.py同目录下创建server.py

from tornado.wsgi import WSGIContainer
from tornado.httpserver import HTTPServer
from tornado.ioloop import IOLoop
from app import app#这里要和run.py对应
http_server = HTTPServer(WSGIContainer(app))
http_server.listen(5000) #flask默认的端口
IOLoop.instance().start()

直接运行python server.py即可,不过这样只能局域网内访问

下面来看一下云部署。

如果是自己写的flask,则云部署前需要生成 requirements 文件:

pip freeze > requirements.txt

2 Procfile

这里使用Heroku平台进行部署。

Heroku 应用程序包含一个Procfile,它指定应用程序在启动时执行的命令。以使用 Procfile 来声明各种进程类型。

Procfile 是一个没有文件扩展名的简单文本文件。

:

是命令的字母数字名称,例如web, worker, urgentworker, clock等等。 表示每个进程在启动时应该执行的命令,例如rake jobs:work

这里使用:

web: gunicorn app:app

3 heroku部署

heroku是一个云部署平台。

首先将代码fork到自己git账号下:

https://github.com/mengjizhiyou/FlaskIntroduction

部署步骤如下:

  • 新建 app

网址:https://dashboard.heroku.com/apps



  • 连接 github


  • 代码部署


  • 点击view

转到:https://herokuflasktesttest.herokuapp.com/

默认情况下,Heroku 应用程序在其 Heroku 域中可用,其格式为[name of app].herokuapp.com,也可以使用自定义域名。



除了上述GitHub 集成部署方式,也可以通过 Heroku git 远程部署代码,详见:

https://devcenter.heroku.com/articles/git

参考:

https://dormousehole.readthedocs.io/en/latest/quickstart.html https://dormousehole.readthedocs.io/en/latest/patterns/templateinheritance.html https://dormousehole.readthedocs.io/en/latest/deploying/wsgi-standalone.html https://blog.csdn.net/weixin_44406200/article/details/104117583 https://devcenter.heroku.com/articles/procfile https://devcenter.heroku.com/articles/custom-domains https://devcenter.heroku.com/articles/github-integration

相关内容