【Python】【Flask】Flask 后台发送html页面多种方法

一、使用模板:css

@app.route('/')
def home():
    return render_template("homepage.html")#homepage.html在templates文件夹下

二、使用 send_from_directoryhtml

root = os.path.join(os.path.dirname(os.path.abspath(__file__)), "html")#html是个文件夹

@app.route('/')
def home():
    return send_from_directory(root, "homepage.html")#homepage.html在html文件夹下

三、使用 app.send_static_filepython

app = Flask(__name__,static_url_path='')#修改静态文件夹的目录

@app.route('/')
def home():
    return app.send_static_file('homepage.html')#homepage.html在static文件夹下

四、flask 调用 css文件web

app = Flask(__name__,static_url_path='') 
@app.route('/')
def home():
return app.send_static_file('html/homepage.html')
 <link rel="stylesheet" type="text/css" href="../css/homepagestyle.css">#html里面引用css

 文件夹结构目录flask

 

注意:css文件必须在静态文件夹下,不然css调用不了浏览器

 

如下内容来自 http://www.javashuo.com/article/p-ukuwgkft-cy.html 只为记录下缓存

flask中的g、add_url_rule、send_from_directory、static_url_path、static_folder的用法

一:Flask中的g对象

    Flask中的g对象是个很好的东西,主要用于在一个请求的过程当中共享数据。能够随意给g对象添加属性来保存数据,很是的方便,下面的代码是一个使用g对象的例子。下面的这个例子会使用random随机产生一个0~9的整数,并使用g.x保存并记录debug日志:微信

# encoding=utf-8
from flask import Flask
from flask import g
import random
 
app = Flask(__name__)
 
@app.before_request
def set_on_g_object():
    x = random.randint(0,9)
    app.logger.debug('before request g.x is {x}'.format(x=x))
    g.x = x
 
@app.route("/")
def test():
    g.x=1000
    return str(g.x)
 
@app.after_request
def get_on_g_object(response):
    app.logger.debug('after request g.x is{g.x}'.format(g=g))
    return response

二:Flask中静态文件的处理

1.add_url_rule的用法

    Flask中提供了url_for来实现建立url,只是生成一个url。在前面的博文中谈论过若是要生成一个css样式的静态文件的url须要使用url_for('static',filename='style.css')来建立相应的url。可是若是我有一个目录attachment的目录存放一些文件的话是无法经过url_for来生成的,默认url_for只能够为static和一些view_func创建url若是要想经过url_for为attachment来添加url就必须添加一个add_url_rule。cookie

# encoding=utf-8
from flask import Flask
from flask import g
from flask import send_from_directory
from flask import url_for
import random
 
app = Flask(__name__)
 
@app.route("/")
def test():
    return "url建立方式一"
 
def hello():
    return "url建立方式二"
 
app.add_url_rule("/index/",endpoint="hello",view_func=hello)
 
@app.route('/url1')
def Create_url1():
    return url_for('static',filename="style.css")
 
app.add_url_rule('/attachment/<path:filename>',endpoint='attachment',build_only=True)
@app.route('/url2')
def Create_url2():
    return url_for('attachment',filename="upload.txt")

2.send_from_directory的用法

send_from_directory主要用于下载文件:session

下面是一个文件的下载实例

# encoding=utf-8
from flask import Flask
from flask import g
from flask import send_from_directory
from flask import url_for
import os.path
 
app = Flask(__name__)
dirpath = os.path.join(app.root_path,'upload')
@app.route("/download/<path:filename>")
def downloader(filename):
    return send_from_directory(dirpath,filename,as_attachment=True)

首选在application下创建一个upload目录,构造upload目录的绝对路径。

而后经过浏览器输入指定文件的文件名来下载。

3.static_url_path和static_folder的用法

static_url_path主要用于改变url的path的,静态文件放在static下面,因此正常状况url是static/filename ,可是能够经过static_url_path来改变这个url

static_folder主要是用来改变url的目录的,默认是static,能够经过这个变量来改变静态文件目录。

# encoding=utf-8
from flask import Flask
from flask import g
from flask import send_from_directory
from flask import url_for
import os.path
 
app = Flask(__name__,static_url_path="/test")
 
@app.route("/")
def static_create():
    return url_for('static',filename='style.css')

4.静态页面缓存和文件索引

SEND_FILE_MAX_AGE_DEFAULT 这个变量用于配置静态文件缓存的时间,Flask默认缓存时间是12hours

例如: app.comfig['SEND_FILE_MAX_AGE_DEFAULT']=2592000 将其缓存时间改成了30天。

Flask不能实现文件索引的功能,也就是没法列出文件名,这个须要web server(Nginx 或 Apache)来实现。

五、session 也是一个 request context 的变量,但它把数据保存到了 cookie 中并发送到了客户端,客户端再次请求的时候又带上了cookie

  

扫码关注微信公众号

微信公众号

相关文章
相关标签/搜索