-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.py
213 lines (191 loc) · 7.42 KB
/
app.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
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
#!/usr/bin/env python3
import dash
from dash.dependencies import Input, Output
import dash_core_components as dcc
import dash_html_components as html
import dash_table
import pandas as pd
import datetime
app = dash.Dash(__name__)
df = pd.read_csv('combinedUsers.csv')
df['RSTUDIO'] = pd.to_datetime(df['RSTUDIO'])
df['ZEPPELIN'] = pd.to_datetime(df['ZEPPELIN'])
df['HOME DIRECTORY'] = pd.to_datetime(df['HOME DIRECTORY'])
df["LAST UPDATE"] = df[["RSTUDIO", "ZEPPELIN", "HOME DIRECTORY"]].max(axis=1)
df["LAST UPDATE"] = df["LAST UPDATE"].dt.strftime('%Y-%m-%d %H:%M')
PAGE_SIZE = 5
tablehome= dash_table.DataTable(
id='table-paging-with-graph',
columns=[
{"name": i, "id": i} for i in df.columns
],
page_current=0,
page_size=25,
page_action='custom',
filter_action='custom',
filter_query='',
sort_action='custom',
sort_mode='single',
sort_by=[],
style_cell_conditional=[
{
'if': {'column_id': c},
'display': 'none'
} for c in ['ZEPPELIN', 'RSTUDIO', 'HOME DIRECTORY']
],
style_table={
'maxHeight': '450px',
'overflowY': 'scroll'
},
style_as_list_view=True,
style_cell={'fontSize':11, 'font-family':'Arial', 'text-align':'left'},
style_header={'backgroundColor': 'white','fontWeight': 'bold', 'text-align':'left'}
)
def serve_layout():
return html.Div(
[
dcc.Store(id="aggregate_data"),
# empty Div to trigger javascript file for graph resizing
html.Div(id="output-clientside"),
html.Div(
[html.Div(
[html.Img(
src=app.get_asset_url("logo.PNG"),
id="plotly-image",
style={
"height": "50px", #"width": "22%",
"marginBottom": "25px",
"marginTop": "25px",
"boxShadow": "2px 2px 2px lightgrey",
},
)
],
className="one-third column",
),
html.Div(
[html.Div(
[html.H3(
"User Management Dashboard",
style={"margin-bottom": "0px"},
),
html.H5("Analytics Platform", style={"margin-top": "0px"}),
]
)
],
className="eight columns",
id="title",
)
],
id="header",
className="row flex-display",
style={"margin-bottom": "25px"},
),
html.Div(
[html.Div(
[html.Div(
[html.Div(
[html.H6(id="well_text"), html.P("Business Users"), html.H5(len(df.index))],
id="wells",
className="mini_container",
),
html.Div(
[html.H6(id="gasText"), html.P("Groups"), html.H5(df['GROUP'].nunique())],
id="gas",
className="mini_container",
),
html.Div(
[html.H6(id="oilText"), html.P("Analytical Tools"),html.H5("2")],
id="oil",
className="mini_container",
)
],
id="info-container",
className="row container-display",
),
],
id="right-column",
className="eight columns",
),
],
className="row flex-display",
),
html.Div(
[html.Div(
[
html.P("Users Last Update"),
tablehome,
html.Br(),
html.P("Current time: "+str(datetime.datetime.now())),
],
className="pretty_container",
),
], className="row flex-display",
),
],
id="mainContainer",
style={"display": "flex", "flexDirection": "column","margin":"auto","maxWidth":"850px"},
)
app.layout= serve_layout
operators = [['ge ', '>='],
['le ', '<='],
['lt ', '<'],
['gt ', '>'],
['ne ', '!='],
['eq ', '='],
['contains '],
['datestartswith ']]
def split_filter_part(filter_part):
for operator_type in operators:
for operator in operator_type:
if operator in filter_part:
name_part, value_part = filter_part.split(operator, 1)
name = name_part[name_part.find('{') + 1: name_part.rfind('}')]
value_part = value_part.strip()
v0 = value_part[0]
if (v0 == value_part[-1] and v0 in ("'", '"', '`')):
value = value_part[1: -1].replace('\\' + v0, v0)
else:
try:
value = float(value_part)
except ValueError:
value = value_part
# word operators need spaces after them in the filter string,
# but we don't want these later
return name, operator_type[0].strip(), value
return [None] * 3
@app.callback(
Output('table-paging-with-graph', "data"),
[Input('table-paging-with-graph', "page_current"),
Input('table-paging-with-graph', "page_size"),
Input('table-paging-with-graph', "sort_by"),
Input('table-paging-with-graph', "filter_query")])
def update_table(page_current, page_size, sort_by, filter):
filtering_expressions = filter.split(' && ')
dff = df
for filter_part in filtering_expressions:
col_name, operator, filter_value = split_filter_part(filter_part)
if operator in ('eq', 'ne', 'lt', 'le', 'gt', 'ge'):
# these operators match pandas series operator method names
dff = dff.loc[getattr(dff[col_name], operator)(filter_value)]
elif operator == 'contains':
dff = dff.loc[dff[col_name].str.contains(filter_value)]
elif operator == 'datestartswith':
# this is a simplification of the front-end filtering logic,
# only works with complete fields in standard format
dff = dff.loc[dff[col_name].str.startswith(filter_value)]
if len(sort_by):
dff = dff.sort_values(
[col['column_id'] for col in sort_by],
ascending=[
col['direction'] == 'asc'
for col in sort_by
],
inplace=False
)
return dff.iloc[
page_current*page_size: (page_current + 1)*page_size
].to_dict('records')
# Main
server = app.server
if __name__ == '__main__':
app.run_server(debug=True)