-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdashboard.py
65 lines (42 loc) · 1.89 KB
/
dashboard.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
from flask import Flask,render_template
import pandas as pd
import json
import plotly
import plotly.express as px
from datetime import datetime
import time
import dash_bootstrap_components as dbc
from dash_bootstrap_templates import load_figure_template
app = Flask(__name__)
def filter_current_month(data):
data['Month'] = data['Month'].astype(str)
data['Year'] = data['Year'].astype(str)
now = datetime.now()
month = now.strftime("%B")
year = str(now.year)
every_this_month = data[(data['Month'] == month) & (data['Year'] == year)]
return every_this_month
def filter_current_year(data):
now = datetime.now()
current_year = now.year
filtered_data = data[data['Year'] == current_year]
return filtered_data
def get_table(offset=0, per_page=10):
return table[offset: offset + per_page]
@app.route('/')
def dashboard():
data = pd.read_csv('random_data.csv')
this_year = filter_current_year(data)
this_month = filter_current_month(data)
load_figure_template('darkly')
line = px.line(this_month, x="Date",y="Mood",template='darkly')
lineJSON = json.dumps(line, cls=plotly.utils.PlotlyJSONEncoder)
bar = px.histogram(this_year, x="Month",color='Mood',barmode='group',template='darkly')
barJSON = json.dumps(bar, cls=plotly.utils.PlotlyJSONEncoder)
mood_per_year = data.groupby('Year')['Mood'].value_counts().reset_index(name='Count')
year = px.line(mood_per_year, x="Year", y="Count", color='Mood',template='darkly')
yearJSON = json.dumps(year, cls=plotly.utils.PlotlyJSONEncoder)
table = data.to_html(classes='table table-striped text-center card-body mt-3',index=False).replace('<tr style="text-align: right;">', '<tr style="text-align: center;">')
return render_template('index.html',tables=[table],line=lineJSON,bar=barJSON,years=yearJSON,title=[''])
if __name__ == "__main__":
app.run(host="localhost",port=8129)