Skip to content

Commit 3bb4f7b

Browse files
committed
add MyShow
1 parent 79c789d commit 3bb4f7b

File tree

12 files changed

+527
-0
lines changed

12 files changed

+527
-0
lines changed

MyShow/GetData_zhihu.py

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
# _*_ coding: utf-8 _*_
2+
3+
import pymysql
4+
5+
con = pymysql.connect(host="xxxx", user="root", passwd="xxxx", db="xxxx", charset="utf8")
6+
cursor = con.cursor()
7+
con.autocommit(1)
8+
9+
10+
def get_all_topics():
11+
cursor.execute("select distinct t_topic_id, t_topic_name from t_zhihutopics where t_topic_haschildren = 1;")
12+
return [item for item in cursor.fetchall() if item[0].strip()]
13+
14+
15+
def get_topic_data(topic_id, topic_name):
16+
data_dict = {
17+
"type": "force",
18+
"nodes": [
19+
{"id": topic_id, "name": topic_name, "level": 0}
20+
],
21+
"links": []
22+
}
23+
24+
nodes_set = set([topic_id])
25+
dai_ids = set([topic_id])
26+
while dai_ids:
27+
cursor.execute("select * from t_zhihutopics where t_topic_parentid = %s;", [dai_ids.pop()])
28+
for item in cursor.fetchall():
29+
_, t_id, t_name, t_pid, t_haschild, _ = item
30+
31+
if t_id not in nodes_set:
32+
nodes_set.add(t_id)
33+
data_dict["nodes"].append({"id": t_id, "name": t_name, "level": 1 if t_pid == topic_id else 2})
34+
data_dict["links"].append({"source": t_pid, "target": t_id})
35+
36+
if t_haschild == 1:
37+
dai_ids.add(t_id)
38+
return data_dict

MyShow/MyShow.py

Lines changed: 100 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,100 @@
1+
# _*_ coding: utf-8 _*_
2+
3+
import logging
4+
import GetData_zhihu
5+
6+
# flask
7+
from flask import Flask, session, request
8+
from flask import render_template, flash, redirect, url_for, jsonify
9+
10+
# flask extends
11+
from flask_bootstrap import Bootstrap
12+
from flask_wtf import FlaskForm
13+
from wtforms import StringField, PasswordField, SubmitField
14+
from wtforms.validators import Length, Email
15+
16+
# application
17+
app = Flask(__name__)
18+
app.config["SECRET_KEY"] = "hard to guess string"
19+
20+
# manager and bootstrap
21+
bootstrap = Bootstrap(app=app)
22+
23+
# global data
24+
zhihu_all_topics = GetData_zhihu.get_all_topics()
25+
zhihu_all_topics_key = {}
26+
zhihu_init_topics = GetData_zhihu.get_topic_data(topic_id="19559424", topic_name="数据分析")
27+
28+
29+
# form class
30+
class UserForm(FlaskForm):
31+
name = StringField("name", validators=[Email(message="邮箱格式不正确!")])
32+
password = PasswordField("password", validators=[Length(min=6, message="密码长度至少6位!")])
33+
submit = SubmitField("提 交")
34+
35+
36+
@app.route("/", methods=["GET", "POST"])
37+
def temp():
38+
return redirect(url_for("index"))
39+
40+
41+
@app.route("/index/", methods=["GET", "POST"])
42+
def index():
43+
user_form = UserForm()
44+
if request.method == "POST":
45+
if user_form.validate_on_submit():
46+
session["username"] = user_form.name.data
47+
else:
48+
flash(user_form.errors["name"][0] if "name" in user_form.errors else user_form.errors["password"][0])
49+
else:
50+
if request.args.get("action") == "login_out":
51+
flash("您已成功退出系统!")
52+
session["username"] = None
53+
return redirect(url_for("index"))
54+
elif request.args.get("action") == "overview":
55+
session["page_type"] = "overview"
56+
return redirect(url_for("index"))
57+
elif request.args.get("action") == "zhihu_topics":
58+
session["page_type"] = "zhihu_topics"
59+
return redirect(url_for("index"))
60+
return render_template("index.html", name=session.get("username"), page_type=session.get("page_type", "overview"), form=user_form)
61+
62+
63+
@app.route("/zhihu_get_topics_list/", methods=["post"])
64+
def zhihu_get_topics_list():
65+
key = request.form.get("key")
66+
result = {"success": 1, "data": []}
67+
if key:
68+
if key in zhihu_all_topics_key:
69+
result = zhihu_all_topics_key[key]
70+
else:
71+
for item in zhihu_all_topics:
72+
if item[1].find(key) >= 0:
73+
result["data"].append({"id": item[0], "name": item[1]})
74+
if len(result["data"]) > 0:
75+
result["success"] = 1
76+
zhihu_all_topics_key[key] = result
77+
logging.debug("all_topics_key increase: %s", len(zhihu_all_topics_key))
78+
return jsonify(result)
79+
80+
81+
@app.route("/zhihu_get_topics_data/", methods=["post"])
82+
def zhihu_get_topics_data():
83+
if request.form["id"] == "19554449":
84+
result = zhihu_init_topics
85+
else:
86+
result = GetData_zhihu.get_topic_data(request.form["id"], request.form["name"])
87+
return jsonify(result)
88+
89+
90+
@app.errorhandler(404)
91+
def page_not_found(excep):
92+
return render_template("error.html", error=excep, name=session.get("username")), 404
93+
94+
95+
# main process
96+
if __name__ == "__main__":
97+
logging.basicConfig(level=logging.DEBUG, format="%(asctime)s\t%(levelname)s\t%(message)s")
98+
logging.debug("app url_map: %s", app.url_map)
99+
100+
app.run()

MyShow/static/css/dashboard.css

Lines changed: 105 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,105 @@
1+
/*
2+
* Base structure
3+
*/
4+
5+
/* Move down content because we have a fixed navbar that is 50px tall */
6+
body {
7+
padding-top: 50px;
8+
}
9+
10+
11+
/*
12+
* Global add-ons
13+
*/
14+
15+
.sub-header {
16+
padding-bottom: 10px;
17+
border-bottom: 1px solid #eee;
18+
}
19+
20+
/*
21+
* Top navigation
22+
* Hide default border to remove 1px line.
23+
*/
24+
.navbar-fixed-top {
25+
border: 0;
26+
}
27+
28+
/*
29+
* Sidebar
30+
*/
31+
32+
/* Hide for mobile, show later */
33+
.sidebar {
34+
display: none;
35+
}
36+
@media (min-width: 768px) {
37+
.sidebar {
38+
position: fixed;
39+
top: 51px;
40+
bottom: 0;
41+
left: 0;
42+
z-index: 1000;
43+
display: block;
44+
padding: 20px;
45+
overflow-x: hidden;
46+
overflow-y: auto; /* Scrollable contents if viewport is shorter than content. */
47+
background-color: #f5f5f5;
48+
border-right: 1px solid #eee;
49+
}
50+
}
51+
52+
/* Sidebar navigation */
53+
.nav-sidebar {
54+
margin-right: -21px; /* 20px padding + 1px border */
55+
margin-bottom: 20px;
56+
margin-left: -20px;
57+
}
58+
.nav-sidebar > li > a {
59+
padding-right: 20px;
60+
padding-left: 20px;
61+
}
62+
.nav-sidebar > .active > a,
63+
.nav-sidebar > .active > a:hover,
64+
.nav-sidebar > .active > a:focus {
65+
color: #fff;
66+
background-color: #428bca;
67+
}
68+
69+
70+
/*
71+
* Main content
72+
*/
73+
74+
.main {
75+
padding: 20px;
76+
}
77+
@media (min-width: 768px) {
78+
.main {
79+
padding-right: 40px;
80+
padding-left: 40px;
81+
}
82+
}
83+
.main .page-header {
84+
margin-top: 0;
85+
}
86+
87+
88+
/*
89+
* Placeholder dashboard ideas
90+
*/
91+
92+
.placeholders {
93+
margin-bottom: 30px;
94+
text-align: center;
95+
}
96+
.placeholders h4 {
97+
margin-bottom: 0;
98+
}
99+
.placeholder {
100+
margin-bottom: 20px;
101+
}
102+
.placeholder img {
103+
display: inline-block;
104+
border-radius: 50%;
105+
}

MyShow/static/favicon.ico

66.1 KB
Binary file not shown.

MyShow/static/js/bootstrap-typeahead.min.js

Lines changed: 1 addition & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

MyShow/static/js/bootstrap.min.js

Lines changed: 7 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)