-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsimple_app.py
executable file
·94 lines (86 loc) · 2.85 KB
/
simple_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
#! /home/nathangray/PycharmProjects/ResultsViewer/venv310/bin/python3.10
from dash import Dash, dcc, html, Input, Output
import dash_bootstrap_components as dbc
import plotly.express as px
import pandas as pd
import io
import base64
from math import sqrt
app = Dash(external_stylesheets=[dbc.themes.YETI])
df = px.data.stocks()
base = sqrt(3)
df = pd.DataFrame # replace with your own data source
# options = list(df.iloc[0, 1:].keys())
# print(options)
app.layout = html.Div([
html.H4('Dataframe plotter'),
dcc.Upload(id='uploader',
children=html.Div(['Select csv: ', html.A('file')]),
multiple=False),
dcc.Graph(id="time-series-chart"),
html.P("Select column:"),
dcc.Input(id="base_input", placeholder="Base", type="number", persistence=True, persistence_type='local'),
dcc.Dropdown(
id="ticker",
# options=options,
# value=options[0],
clearable=False,
multi=True
),
])
@app.callback(
[
Output('ticker', 'options'),
Output('uploader', 'children'),
],
[
Input('uploader', 'contents'),
Input('uploader', 'filename'),
]
)
def import_data1(contents, filename):
global df, options
if contents is not None:
contents = contents
filename = filename
df = parse_data(contents, filename)
options = list(df.iloc[0, 1:].keys())
return options, html.Div([html.A(filename)])
else:
return options, html.Div(['Data: ', html.A('select')])
@app.callback(
Output("time-series-chart", "figure"),
[
Input("ticker", "value"),
Input("base_input", "value")
]
)
def display_time_series(ticker, base):
global df
print(ticker)
_df = df.copy()
_df[ticker] = df[ticker]/(base/sqrt(3))
fig = px.line(_df, y=ticker, labels={'variable': 'Node'})
fig.update_xaxes(title_text="Time (s)")
fig.update_yaxes(title_text="Voltage (p.u.)")
return fig
def parse_data(contents, filename):
content_type, content_string = contents.split(",")
df_import = None
decoded = base64.b64decode(content_string)
try:
if "csv" in filename:
# Assume that the user uploaded a CSV or TXT file
df_import = pd.read_csv(io.StringIO(decoded.decode("utf-8")), sep=',', header=8, index_col=0, parse_dates=True)
elif "xls" in filename:
# Assume that the user uploaded an excel file
df_import = pd.read_excel(io.BytesIO(decoded))
elif "txt" or "tsv" in filename:
# Assume that the user upl, delimiter = r'\s+'oaded an excel file
df_import = 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_import
if __name__ == '__main__':
app.run_server(debug=False)