-
Notifications
You must be signed in to change notification settings - Fork 0
/
2222V3.py
333 lines (280 loc) · 10.4 KB
/
2222V3.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
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
import base64
import datetime
import io
import plotly.graph_objs as go
import dash
from dash.dependencies import Input, Output, State
import dash_core_components as dcc
import dash_html_components as html
import dash_table
import pandas as pd
from sqlalchemy import create_engine
# INSERT INTO "data1.db" (data, month, meter) VALUES (6666,'11-UU',844)
colors = {"graphBackground": "#F5F5F5", "background": "#ffffff", "text": "#000000"}
engine = create_engine('sqlite:///data.db', echo=False)
# Dash
def generate_table(dataframe, max_rows=10):
return html.Table(
# Header
[html.Tr([html.Th(col) for col in dataframe.columns])] +
# Body
[html.Tr([
html.Td(dataframe.iloc[i][col]) for col in dataframe.columns
]) for i in range(min(len(dataframe), max_rows))]
)
app = dash.Dash()
app.layout = html.Div([
dcc.Upload(
id="upload-data",
children=html.Div(["Drag and Drop or ", html.A("Select Files")]),
style={
"width": "100%",
"height": "60px",
"lineHeight": "60px",
"borderWidth": "1px",
"borderStyle": "dashed",
"borderRadius": "5px",
"textAlign": "center",
"margin": "10px",
},
# Allow multiple files to be uploaded
multiple=True,
),
html.P(id='saveSql', style={'display':'none'}),
dcc.Input(
id='sql-query',
value='SELECT * FROM "floor1.db"',
style={'width': '100%', 'display':'none'},
type='text'
),
html.Button('Read Data', id='run-query'),
html.Hr(),
html.Div([
html.Div(id='table-container', className="four columns", style={'display': 'none'}),
html.Div([
html.Div([
html.Div([
html.Label('Select X'),
dcc.Dropdown(
id='dropdown-x',
clearable=False,
)
], className="six columns"),
html.Div([
html.Label('Select Y'),
dcc.Dropdown(
id='dropdown-y',
clearable=False,
)
], className="six columns")
], className="row"),
html.Div(dcc.Graph(id='graph'), className="ten columns")
], className="eight columns")
], className="row"),
# hidden store element
html.Div(id='table-store', style={'display': 'none'}),
#################################################################
html.Div([
dcc.Input(
id='adding-rows-name',
placeholder='Enter a column name...',
value='',
style={'padding': 10}
),
html.Button('Add Column', id='adding-columns-button', n_clicks=0)
], style={'height': 50}),
dcc.Interval(id='interval_pg', interval=86400000 * 7, n_intervals=0),
# activated once/week or when page refreshed
html.Div(id='postgres_datatable'),
html.Button('Add Row', id='editing-rows-button', n_clicks=0),
html.Button('Save to PostgreSQL', id='save_to_postgres', n_clicks=0),
# Create notification when saving to excel
html.Div(id='placeholder', children=[]),
dcc.Store(id="store", data=0),
dcc.Interval(id='interval', interval=1000)
])
@app.callback(Output('saveSql', 'children'), [
Input('upload-data', 'contents'),
Input('upload-data', 'filename')
])
def update_graph(contents, filename):
if contents:
contents = contents[0]
filename = filename[0]
df = parse_data(contents, filename)
df = df.set_index(df.columns[0])
df.to_sql('floor1.db', con=engine, if_exists='replace')
def parse_data(contents, filename):
content_type, content_string = contents.split(",")
decoded = base64.b64decode(content_string)
try:
if "csv" in filename:
df = pd.read_csv(io.StringIO(decoded.decode("utf-8")))
elif "xls" in filename:
df = pd.read_excel(io.BytesIO(decoded))
elif "txt" or "tsv" in filename:
df = pd.read_csv(io.StringIO(decoded.decode("utf-8")), delimiter=r"\s+")
except Exception as e:
print(e)
return html.Div(["There was an error processing this file."])
return df
def update_table(contents, filename):
table = html.Div()
if contents:
contents = contents[0]
filename = filename[0]
df = parse_data(contents, filename)
table = html.Div(
[
html.H5(filename),
dash_table.DataTable(
data=df.to_dict("rows"),
columns=[{"name": i, "id": i} for i in df.columns],
),
html.Hr(),
html.Div("Raw Content"),
html.Pre(
contents[0:200] + "...",
style={"whiteSpace": "pre-wrap", "wordBreak": "break-all"},
),
]
)
return table
@app.callback(
dash.dependencies.Output('table-store', 'children'),
[dash.dependencies.Input('run-query', 'n_clicks')],
state=[dash.dependencies.State('sql-query', 'value')])
def sql(number_of_times_button_has_been_clicked, sql_query):
dff = pd.read_sql_query(
sql_query,
engine
)
return dff.to_json()
@app.callback(
dash.dependencies.Output('table-container', 'children'),
[dash.dependencies.Input('table-store', 'children')])
def dff_to_table(dff_json):
dff = pd.read_json(dff_json)
return generate_table(dff)
@app.callback(
dash.dependencies.Output('graph', 'figure'),
[dash.dependencies.Input('table-store', 'children'),
dash.dependencies.Input('dropdown-x', 'value'),
dash.dependencies.Input('dropdown-y', 'value')])
def dff_to_table(dff_json, dropdown_x, dropdown_y):
dff = pd.read_json(dff_json)
return {
'data': [{
'x': dff[dropdown_x],
'y': dff[dropdown_y],
'type': 'bar'
}],
'layout': {
'margin': {
'l': 20,
'r': 10,
'b': 60,
't': 10
}
}
}
@app.callback(
dash.dependencies.Output('dropdown-x', 'options'),
[dash.dependencies.Input('table-store', 'children')])
def create_options_x(dff_json):
dff = pd.read_json(dff_json)
return [{'label': i, 'value': i} for i in dff.columns]
@app.callback(
dash.dependencies.Output('dropdown-y', 'options'),
[dash.dependencies.Input('table-store', 'children')])
def create_options_y(dff_json):
dff = pd.read_json(dff_json)
return [{'label': i, 'value': i} for i in dff.columns]
############################################################
@app.callback(Output('postgres_datatable', 'children'),
[Input('interval_pg', 'n_intervals')])
def populate_datatable(n_intervals):
df = pd.read_sql_table('floor1.db', con=engine)
return [
dash_table.DataTable(
id='our-table',
columns=[{
'name': str(x),
'id': str(x),
'deletable': False,
} if x == 'Month'
else {
'name': str(x),
'id': str(x),
'deletable': True,
}
for x in df.columns],
data=df.to_dict('records'),
editable=True,
row_deletable=True,
filter_action="native",
sort_action="native", # give user capability to sort columns
sort_mode="single", # sort across 'multi' or 'single' columns
page_action='none', # render all of the data at once. No paging.
style_table={'height': '300px', 'overflowY': 'auto'},
style_cell={'textAlign': 'left', 'minWidth': '100px', 'width': '100px', 'maxWidth': '100px'},
style_cell_conditional=[
{
'if': {'column_id': c},
'textAlign': 'right'
} for c in ['Month']
]
),
]
@app.callback(
Output('our-table', 'columns'),
[Input('adding-columns-button', 'n_clicks')],
[State('adding-rows-name', 'value'),
State('our-table', 'columns')],
prevent_initial_call=True)
def add_columns(n_clicks, value, existing_columns):
if n_clicks > 0:
existing_columns.append({
'name': value, 'id': value,
'renamable': True, 'deletable': True
})
return existing_columns
@app.callback(
Output('our-table', 'data'),
[Input('editing-rows-button', 'n_clicks')],
[State('our-table', 'data'),
State('our-table', 'columns')],
prevent_initial_call=True)
def add_row(n_clicks, rows, columns):
if n_clicks > 0:
rows.append({c['id']: '' for c in columns})
return rows
@app.callback(
[Output('placeholder', 'children'),
Output("store", "data")],
[Input('save_to_postgres', 'n_clicks'),
Input("interval", "n_intervals")],
[State('our-table', 'data'),
State('store', 'data')],
prevent_initial_call=True)
def df_to_csv(n_clicks, n_intervals, dataset, s):
output = html.Plaintext("The data has been saved to database.",
style={'color': 'green', 'font-weight': 'bold', 'font-size': 'large'})
no_output = html.Plaintext("", style={'margin': "0px"})
input_triggered = dash.callback_context.triggered[0]["prop_id"].split(".")[0]
if input_triggered == "save_to_postgres":
s = 6
pg = pd.DataFrame(dataset)
pg.to_sql('floor1.db', con=engine, if_exists='replace', index=False)
return output, s
elif input_triggered == 'interval' and s > 0:
s = s - 1
if s > 0:
return output, s
else:
return no_output, s
elif s == 0:
return no_output, s
app.css.append_css({"external_url": "https://codepen.io/chriddyp/pen/bWLwgP.css"})
if __name__ == '__main__':
app.run_server(debug=True)