戒酒的李白

【app.py】路径配置

1 from flask import Flask,session,request,redirect,render_template 1 from flask import Flask,session,request,redirect,render_template
2 - 2 +import re
3 app = Flask(__name__) 3 app = Flask(__name__)
  4 +app.secret_key = 'this is secret_key you know ?'
  5 +
  6 +from views.page import page
  7 +from views.user import user
  8 +app.register_blueprint(page.pb)
  9 +app.register_blueprint(user.ub)
4 10
5 @app.route('/') 11 @app.route('/')
6 def hello_world(): # put application's code here 12 def hello_world(): # put application's code here
7 return session.clear() 13 return session.clear()
8 14
  15 +@app.before_request
  16 +def before_reuqest():
  17 + pat = re.compile(r'^/static')
  18 + if re.search(pat,request.path):return
  19 + elif request.path == '/user/login' or request.path == '/user/register':return
  20 + elif session.get('username'):return
  21 + return redirect('/user/login')
  22 +
9 @app.route('/<path:path>') 23 @app.route('/<path:path>')
10 def catch_all(path): 24 def catch_all(path):
11 return render_template('404.html') 25 return render_template('404.html')
12 26
13 if __name__ == '__main__': 27 if __name__ == '__main__':
14 - app.run()  
  28 + app.run()
  1 +import jieba
  2 +from wordcloud import WordCloud
  3 +import matplotlib.pyplot as plt
  4 +from PIL import Image,ImageDraw
  5 +from pymysql import *
  6 +import json
  7 +import numpy as np
  8 +def stopWordList():
  9 + return [line.strip() for line in open('./model/stopWords.txt',encoding='utf8').readlines()]
  10 +
  11 +def get_img(field,tableName,targetImgSrc,resImgSrc):
  12 + con = connect(host='localhost',user='root',password='root',database='weiboarticles',port=3306,charset='utf8mb4')
  13 + cuser = con.cursor()
  14 + sql = f'select {field} from {tableName}'
  15 + cuser.execute(sql)
  16 + data = cuser.fetchall()
  17 + text = ''
  18 + for item in data:
  19 + text += item[0]
  20 + cuser.close()
  21 + con.close()
  22 +
  23 + cut = jieba.cut(text)
  24 + newCut = []
  25 + for word in cut:
  26 + if word not in stopWordList():newCut.append(word)
  27 + string = ' '.join(newCut)
  28 +
  29 + img = Image.open(targetImgSrc)
  30 + img_arr = np.array(img)
  31 + wc = WordCloud(
  32 + background_color="#fff",
  33 + mask=img_arr,
  34 + font_path='STHUPO.TTF'
  35 + )
  36 + wc.generate_from_text(string)
  37 +
  38 + fig = plt.figure(1)
  39 + plt.imshow(wc)
  40 +
  41 + plt.axis('off')
  42 +
  43 + plt.savefig(resImgSrc,dpi=500)
  44 +
  45 +
  46 +# get_img('content','comments','./static/comment.jpg','./static/commentCloud.jpg')
  47 +get_img('content','article','./static/content.jpg','./static/contentCloud.jpg')