1.将多工作表分开
2.数据库建表
3.利用pandas存入数据库
file_path = "data/其他.xlsx" df = pd.read_excel(file_path) print(df.shape[0]) print(df.iloc[0,:]["content"]) for i in range(df.shape[0]): insert_into_mysql(df.iloc[i,:]["content"], df.iloc[i,:]["channelName"], df.iloc[i,:]["title"]) pass def insert_into_mysql(content,channelName,title): conn,cursor=get_conn_mysql() sql="insert into newdata (content,channelName,title) values(%s,%s,%s)" cursor.execute(sql,[content,channelName,title]) conn.commit() close_conn_mysql(conn,cursor) def get_conn_mysql(): """ :return: 连接,游标192.168.1.102 """ # 创建连接 conn = pymysql.connect(host="127.0.0.1", user="root", password="123456", db="new_class", charset="utf8") # 创建游标 cursor = conn.cursor() # 执行完毕返回的结果集默认以元组显示 return conn, cursor def close_conn_mysql(conn, cursor): if cursor: cursor.close() if conn: conn.close()
使用layui实现树形结构
Html:
<div class="layui-body"> <!-- 内容主体区域 --> <div id="test1" class="demo-tree-more"></div> <div id="bar" style=" 1000px;height: 600px"></div> </div>
Js部分
注意data_deep1的数据格式:
[//@@@@@@@@@@@@@@ { title: '体育' //二级菜单 ,children: [{ title: '高新区' //三级菜单 ,href: 'https://www.layui.com/doc/' //…… //以此类推,可无限层级 },{}] },{ title: '陕西' //一级菜单 ,children: [{ title: '西安' //二级菜单 ,href: 'https://www.layui.com/doc/' }] },{ title: '陕西' //一级菜单 ,children: [{ title: '西安' //二级菜单 ,href: 'https://www.layui.com/doc/' }] },{ title: '陕西' //一级菜单 ,children: [{ title: '西安' //二级菜单 ,href: 'https://www.layui.com/doc/' }] }] $.ajax({ type: "GET", url: "/tree", dataType: "json", success: function(data){ data_deep2=[] data_deep1=[] data_deep2=set_deep2(data.a) data_deep1[0]={title:"体育(200)",children:data_deep2} data_deep2=set_deep2(data.b) data_deep1[1]={title:"综合体育最新(1000)",children:data_deep2} data_deep2=set_deep2(data.c) data_deep1[2]={title:"军事(153)",children:data_deep2} data_deep2=set_deep2(data.d) data_deep1[3]={title:"娱乐(794)",children:data_deep2} data_deep2=set_deep2(data.e) data_deep1[4]={title:"体育焦点(400)",children:data_deep2} data_deep2=set_deep2(data.f) data_deep1[5]={title:"房产(200)",children:data_deep2} data_deep2=set_deep2(data.g) data_deep1[6]={title:"教育(499)",children:data_deep2} data_deep2=set_deep2(data.h) data_deep1[7]={title:"汽车(646)",children:data_deep2} data_deep2=set_deep2(data.i) data_deep1[8]={title:"游戏(1276)",children:data_deep2} data_deep2=set_deep2(data.j) data_deep1[9]={title:"科技(830)",children:data_deep2} data_deep2=set_deep2(data.k) data_deep1[10]={title:"财经(8530)",children:data_deep2} layui.use('tree', function(){ var tree = layui.tree; //渲染 var inst1 = tree.render({ elem: '#test1' //绑定元素 ,isJump: true ,data: [{ title: '新闻' //一级菜单 ,children: data_deep1 }] }); }); } }); function set_deep2(value_data){ data_deep2_flag=[] for(i=0;i<value_data.length;i++){ data_deep2_flag[i]={title:value_data[i],href:"/tree_details?title="+value_data[i]+""} } return data_deep2_flag }
后台代码
@app.route('/tree') def tree(): a=[] b=[] c=[] d=[] e=[] f=[] g=[] h=[] i=[] j=[] k=[] a=test.deep_2("体育") b=test.deep_2("综合体育最新") c=test.deep_2("军事") d=test.deep_2("娱乐") e=test.deep_2("体育焦点") f=test.deep_2("房产") g=test.deep_2("教育") h=test.deep_2("汽车") i=test.deep_2("游戏") j=test.deep_2("科技") k=test.deep_2("财经") print(i) return jsonify({"a": a, "b": b,"c":c,"d":d,"e":e,"f":f,"g":g,"h":h,"i":i,"j":j,"k":k}) pass
效果:
点击后进入详情页面
详情页面的js:
var chart = echarts.init(document.getElementById('main')); var option = { tooltip: {}, series: [ { type: 'wordCloud', gridSize: 2, sizeRange: [12, 50], rotationRange: [-90, 90], shape: 'pentagon', 600, height: 400, drawOutOfBound: true, textStyle: { color: function () { return 'rgb(' + [ Math.round(Math.random() * 160), Math.round(Math.random() * 160), Math.round(Math.random() * 160) ].join(',') + ')'; } }, emphasis: { textStyle: { shadowBlur: 10, shadowColor: '#333' } }, data: [ { name: 'Sam S Club', value: 10000, }, { name: 'Macys', value: 6181 }, { name: 'Amy Schumer', value: 4386 }, { name: 'Jurassic World', value: 4055 }, { name: 'Charter Communications', value: 2467 }, { name: 'Chick Fil A', value: 2244 }, { name: 'Planet Fitness', value: 1898 }, { name: 'Pitch Perfect', value: 1484 }, { name: 'Express', value: 1112 }, { name: 'Home', value: 965 }, { name: 'Johnny Depp', value: 847 }, { name: 'Lena Dunham', value: 582 }, { name: 'Lewis Hamilton', value: 555 }, { name: 'KXAN', value: 550 }, { name: 'Mary Ellen Mark', value: 462 }, { name: 'Farrah Abraham', value: 366 }, { name: 'Rita Ora', value: 360 }, { name: 'Serena Williams', value: 282 }, { name: 'NCAA baseball tournament', value: 273 }, { name: 'Point Break', value: 265 } ] }] }; chart.setOption(option); window.onresize = chart.resize; $.ajax({ url: "/word_cloud_date?title={{title}}", success: function (data) { option.series[0].data=data.data; chart.setOption(option); }, error: function (xhr, type, errorThrown) { } })
后台:
#词云图数据 @app.route("/word_cloud_date") def word_cloud_date(): title_name = request.values.get("title") values = test.find_content_by_title(title_name) word_top=until.word_top(values[0][0]) print(word_top) word_cloud_num=[] flag=0; for i in word_top: word_cloud_num.append({"name":i[0],"value":i[1]}) flag=flag+1; if(flag>30): break return jsonify({"data":word_cloud_num}) def word_top(str): dele = {'。','!','?','的','“','”','(',')',' ','》','《',','} jieba.add_word('大数据') words = list(jieba.cut(str)) articleDict = {} articleSet = set(words)-dele for w in articleSet: if len(w)>1: articleDict[w] = words.count(w) articlelist = sorted(articleDict.items(),key = lambda x:x[1], reverse = True) return articlelist