【app.py】路径配置
This commit is contained in:
@@ -1,14 +1,28 @@
|
|||||||
from flask import Flask,session,request,redirect,render_template
|
from flask import Flask,session,request,redirect,render_template
|
||||||
|
import re
|
||||||
app = Flask(__name__)
|
app = Flask(__name__)
|
||||||
|
app.secret_key = 'this is secret_key you know ?'
|
||||||
|
|
||||||
|
from views.page import page
|
||||||
|
from views.user import user
|
||||||
|
app.register_blueprint(page.pb)
|
||||||
|
app.register_blueprint(user.ub)
|
||||||
|
|
||||||
@app.route('/')
|
@app.route('/')
|
||||||
def hello_world(): # put application's code here
|
def hello_world(): # put application's code here
|
||||||
return session.clear()
|
return session.clear()
|
||||||
|
|
||||||
|
@app.before_request
|
||||||
|
def before_reuqest():
|
||||||
|
pat = re.compile(r'^/static')
|
||||||
|
if re.search(pat,request.path):return
|
||||||
|
elif request.path == '/user/login' or request.path == '/user/register':return
|
||||||
|
elif session.get('username'):return
|
||||||
|
return redirect('/user/login')
|
||||||
|
|
||||||
@app.route('/<path:path>')
|
@app.route('/<path:path>')
|
||||||
def catch_all(path):
|
def catch_all(path):
|
||||||
return render_template('404.html')
|
return render_template('404.html')
|
||||||
|
|
||||||
if __name__ == '__main__':
|
if __name__ == '__main__':
|
||||||
app.run()
|
app.run()
|
||||||
|
|||||||
@@ -0,0 +1,47 @@
|
|||||||
|
import jieba
|
||||||
|
from wordcloud import WordCloud
|
||||||
|
import matplotlib.pyplot as plt
|
||||||
|
from PIL import Image,ImageDraw
|
||||||
|
from pymysql import *
|
||||||
|
import json
|
||||||
|
import numpy as np
|
||||||
|
def stopWordList():
|
||||||
|
return [line.strip() for line in open('./model/stopWords.txt',encoding='utf8').readlines()]
|
||||||
|
|
||||||
|
def get_img(field,tableName,targetImgSrc,resImgSrc):
|
||||||
|
con = connect(host='localhost',user='root',password='root',database='weiboarticles',port=3306,charset='utf8mb4')
|
||||||
|
cuser = con.cursor()
|
||||||
|
sql = f'select {field} from {tableName}'
|
||||||
|
cuser.execute(sql)
|
||||||
|
data = cuser.fetchall()
|
||||||
|
text = ''
|
||||||
|
for item in data:
|
||||||
|
text += item[0]
|
||||||
|
cuser.close()
|
||||||
|
con.close()
|
||||||
|
|
||||||
|
cut = jieba.cut(text)
|
||||||
|
newCut = []
|
||||||
|
for word in cut:
|
||||||
|
if word not in stopWordList():newCut.append(word)
|
||||||
|
string = ' '.join(newCut)
|
||||||
|
|
||||||
|
img = Image.open(targetImgSrc)
|
||||||
|
img_arr = np.array(img)
|
||||||
|
wc = WordCloud(
|
||||||
|
background_color="#fff",
|
||||||
|
mask=img_arr,
|
||||||
|
font_path='STHUPO.TTF'
|
||||||
|
)
|
||||||
|
wc.generate_from_text(string)
|
||||||
|
|
||||||
|
fig = plt.figure(1)
|
||||||
|
plt.imshow(wc)
|
||||||
|
|
||||||
|
plt.axis('off')
|
||||||
|
|
||||||
|
plt.savefig(resImgSrc,dpi=500)
|
||||||
|
|
||||||
|
|
||||||
|
# get_img('content','comments','./static/comment.jpg','./static/commentCloud.jpg')
|
||||||
|
get_img('content','article','./static/content.jpg','./static/contentCloud.jpg')
|
||||||
Reference in New Issue
Block a user