0
点赞
收藏
分享

微信扫一扫

flask框架

史值拥 2024-11-18 阅读 17

flask:

new project

记得开启debug模式, \* Debugger is active!方便修改能及时传到服务器(保存)

from flask import Flask

app = Flask(\__name_\_)

# 路由解析,通过用户访问的路径,匹配相应的函数

@app.route('/index')#访问路径为<http://127.0.0.1:5000/index>

def hello_world():

return 'nihao'

if \__name__ == '\__main_\_':

app.run()

@app.route('/index/<name>')#<name>接受用户输入内容(也可以用其它字符表示),中英文均可

def hello(name):

return 'nihao,%s'%name

@app.route('/index/<int:id>')#<int:id>,整型参数

def hello(id):

return 'nihao,%d'%id

# 输入不同,匹配不同的函数,路由路径不能重复

@app.route('/index/<float:id>')#<float:id>,浮点型参数

def hello(id):

return 'nihao,%f'%id

from flask import Flask,render_template

app = Flask(\__name_\_)

# 给用户返回渲染后的html文件

@app.route('/')#<float:id>,浮点型参数

def index():

return render_template("test.html")

if \__name__ == '\__main_\_':

app.run()

app.py

from flask import Flask,render_template,request

import datetime

app = Flask(\__name_\_)

# 传递变量var时间

# 点击图像不同部位,跳转到不同的链接,时间:{{ var }},在HTML文件中用“{{}}”表示变量

@app.route('/')

def index():

time=datetime.datetime.today()#普通变量

name=\["orange","apple"\]#列表

age={"Tom":15,"Jarry":24}#字典

return render_template("test.html",var=time,fruits=name,age=age)#给var赋予时间变量

# 表单提交

@app.route('/register')

def register():

return render_template("register.html")

# 接收表单提交的路由,需要指定menthod为post

# 表单数据可以作为 URL 变量(method="get")或者 HTTP post (method="post")的方式来发送。

@app.route('/result',methods=\["post","get"\])

def result():

if request.method=="POST":#注意post大写据baiRFC2616,HTTP Method是区分du大小写的,而zhiHeader是不dao区分的。】所以 GET/POST/PUT等需要大写

result=request.form#将表单形成字典

return render_template("result.html",result=result)

if \__name__ == '\__main_\_':

app.run()

result.html

<!DOCTYPE html>

<html lang="en">

<head>

<meta charset="UTF-8">

<title>Title</title>

</head>

<body>

<table border="1">

{# 字典.items()会使字典变成列表\[(key,value),(key,value),(key,value)\]#}

{% for key,value in result.items() %}

<tr>

<th>{{ key }}</th>

<td>{{ value }}</td>

</tr>

{% endfor %}

</table>

</body>

</html>

register.html

<!DOCTYPE html>

<html lang="en">

<head>

<meta charset="UTF-8">

<title>Title</title>

</head>

<body>

<form action="{{ url_for('result')}}" method="post">

{# name的值表示键值对的键值,url_for()自动获取路由位置,转而执行result函数#}

<p>姓名:<input type="text" name="姓名"></p>

<p>性别:<input type="text" name="性别"></p>

<p>年龄:<input type="text" name="年龄"></p>

<p>地址:<input type="text" name="地址"></p>

<p><input type="submit" value="提交"></p>

</form>

</body>

</html>

test.html

<!DOCTYPE html>

<html>

<head>

<title>叶周兵</title>

</head>

<body>

{#用大括号与%括起来的是控制语句#}

<p>点击图像不同部位,跳转到不同的链接,时间:{{ var }}<br/>

水果:<br/>

{% for data in fruits %}

<li>{{ data }}</li>

{% endfor %}

</p>

年龄:<br/>

<table border="1">

{# 字典.items()会使字典变成列表\[(key,value),(key,value),(key,value)\]#}

{% for key,value in age.items() %}

<tr>

<td>{{ key }}</td>

<td>{{ value }}</td>

</tr>

{% endfor %}

</table>

</body>

</html>

html文件均保存在templates文件夹中,例如assets保存在static中(css、js等)

wordcloud:

import jieba#用于分词

from matplotlib import pyplot as plt#绘图,数据可视化

from wordcloud import WordCloud#词云,注意大小写

from PIL import Image#用于图形处理

import numpy as np#用于矩阵运算

import sqlite3

# 准备词云所需的文字

con=sqlite3.connect("douban.db")

cur=con.cursor()

sql="select sentence from douban_250"

data=cur.execute(sql)

text=""

for item in data:

text=text+item\[0\]

cur.close()

con.close()

# 分词

cut=jieba.cut(text)

string=" ".join(cut)

\# print(string)

# 生成遮罩图片

img=Image.open(r'C:\\Users\\yezhoubing\\Desktop\\前端素材\\tree.jpg')#打开遮罩图片

img_array=np.array(img)#将图片转换成数组(图片背景要纯净)

wc=WordCloud(

\# 这是输出的设置

background_color="#99FFFF",

mask=img_array,

font_path="simkai.ttf"#注意字体中英文

)

wc.generate_from_text(string)

# 绘制图片

fig=plt.figure(1)#创建第一个图像,新建一个名叫 Figure1的画图窗口

plt.imshow(wc)#用wc规则显示

plt.axis("off")#是否显示坐标轴

\# plt.show()#显示生成的词云图片,在Plots窗口中

# 输出词云图片到文件

plt.savefig(r'C:\\Users\\yezhoubing\\Desktop\\前端素材\\tree_word.jpg',dpi=600)#保存为tree_word.jpg,分辨率dpi默认为400

举报

相关推荐

0 条评论