Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit eff7df0

Browse files
authored
Add files via upload
1 parent eddbbb9 commit eff7df0

File tree

15 files changed

+14185
-0
lines changed

15 files changed

+14185
-0
lines changed

python-flask-xchart/app.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
from flask import Flask
2+
3+
app = Flask(__name__)

python-flask-xchart/db.py

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
from app import app
2+
from flaskext.mysql import MySQL
3+
4+
mysql = MySQL()
5+
6+
# MySQL configurations
7+
app.config['MYSQL_DATABASE_USER'] = 'root'
8+
app.config['MYSQL_DATABASE_PASSWORD'] = 'root'
9+
app.config['MYSQL_DATABASE_DB'] = 'roytuts'
10+
app.config['MYSQL_DATABASE_HOST'] = 'localhost'
11+
mysql.init_app(app)

python-flask-xchart/main.py

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
import pymysql
2+
from app import app
3+
from db import mysql
4+
from flask import jsonify, request, render_template
5+
6+
@app.route('/')
7+
def home():
8+
return render_template('xchart.html')
9+
10+
@app.route('/xchart', methods=['POST'])
11+
def x_chart():
12+
conn = None
13+
cursor = None
14+
15+
_json = request.json
16+
start_date = _json['start']
17+
end_date = _json['end']
18+
19+
try:
20+
conn = mysql.connect()
21+
cursor = conn.cursor(pymysql.cursors.DictCursor)
22+
23+
sql = "SELECT SUM(no_of_visits) total_visits, DATE(access_date) day_date FROM site_log WHERE DATE(access_date) >= %s AND DATE(access_date) <= %s GROUP BY DATE(access_date) ORDER BY DATE(access_date) DESC";
24+
25+
param = (start_date, end_date)
26+
27+
cursor.execute(sql, param)
28+
29+
rows = cursor.fetchall()
30+
31+
data = []
32+
33+
for row in rows:
34+
data.append({'label':row['day_date'], 'value':int(row['total_visits'])})
35+
36+
resp = jsonify(data)
37+
38+
resp.status_code = 200
39+
40+
return resp
41+
42+
except Exception as e:
43+
print(e)
44+
45+
finally:
46+
if cursor and conn:
47+
cursor.close()
48+
conn.close()
49+
50+
if __name__ == "__main__":
51+
app.run()

python-flask-xchart/readme.rst

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
You can go through the tutorial https://www.roytuts.com/ajax-jquery-based-xchart-example-using-python-flask-mysql/

0 commit comments

Comments
 (0)