-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathapp.py
336 lines (308 loc) · 11.4 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
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
334
335
336
import dash_mantine_components as dmc
from dash import Dash, html, callback, Input, Output, dcc, dash_table
import base64
import pandas as pd
import dash_table
import dash_pivottable
import io
import plotly.express as px
app = Dash(__name__)
app.layout = dmc.MantineProvider(
[
dmc.Header(
height="10%",
children=[
html.Div(
[
html.Div(
[
dmc.Title(
"Mito for Dash",
order=1,
style={
"text-align": "left",
"margin-bottom": "10px",
"color": "#333",
},
),
dmc.Title(
"Portfolio Analysis Example - without Mito",
order=3,
style={
"text-align": "left",
"color": "#555",
"font-weight": "normal",
},
),
],
style={"flex": "1", "padding": "10px"},
),
html.Div(
[
dcc.Upload(
id="upload-data",
children=[
html.I(
className="fa fa-upload"
), # Using Font Awesome icon
html.Span(" Upload files"),
],
style={
"display": "inline-block",
"width": "auto",
"height": "40px",
"lineHeight": "40px",
"borderWidth": "1px",
"borderStyle": "solid",
"borderColor": "#ccc",
"borderRadius": "5px",
"textAlign": "center",
"margin": "10px",
"padding": "0 15px",
"cursor": "pointer",
"background-color": "#f7f7f7",
},
# Allow multiple files to be uploaded
multiple=True,
),
],
style={"text-align": "right", "padding": "10px"},
),
],
style={
"display": "flex",
"justify-content": "space-between",
"align-items": "center",
"background-color": "#f9f9f9",
"box-shadow": "0px 2px 5px rgba(0, 0, 0, 0.1)",
"border-bottom": "1px solid #eee",
},
),
],
style={"backgroundColor": "#f6e5ff"},
),
html.Div(
[
dcc.Markdown(
"""
### Using this app
1. Click the "Upload Files" button in the upper right corner of this app.
2. Upload the Tesla Stock and S&P500 data linked above from your Downloads folder.
3. When uploaded, scroll below to see automatically generated graphs and a correlation table. **Note** - _this will only work for these two datasets_.
"""
),
],
style={
"padding": "10px",
"margin": "auto",
"maxWidth": "80%",
"font-size": "1.2em",
},
),
dmc.Center(
[
html.Div(
className="pivot-container",
children=[
dash_pivottable.PivotTable(
id="pivot-table",
# ... (keep the rest of your settings here)
),
],
),
]
),
dmc.Center(
id="data_analysis_title",
children=[],
style={
"padding": "10px"
}, # Add some padding around the Center for better spacing
),
html.Div(id="graph-output"), # Container for the graphs
dash_table.DataTable(id="correlation-table"),
]
)
def empty_dataframe_list():
return [["No Data"]]
def empty_div():
return html.Div("")
@callback(
Output("graph-output", "children"),
Output("pivot-table", "data"),
Output("data_analysis_title", "children"),
Input("upload-data", "contents"),
)
def update_output(uploaded_contents):
if uploaded_contents is None or len(uploaded_contents) != 2:
return (
empty_div(),
empty_dataframe_list(),
html.Div(),
)
dataframes = []
for content in uploaded_contents:
content_type, content_string = content.split(",")
decoded = base64.b64decode(content_string)
try:
# Try UTF-8 decoding first
df = pd.read_csv(io.StringIO(decoded.decode("utf-8")))
except UnicodeDecodeError:
try:
# If UTF-8 fails, try ISO-8859-1 decoding
df = pd.read_csv(io.StringIO(decoded.decode("ISO-8859-1")))
except:
return (
empty_div(),
empty_dataframe_list(),
html.Div(),
)
dataframes.append(df)
# Define column sets for each schema
schema1_columns = set(
["Date", "open_sp", "high_sp", "low_sp", "close_sp", "volume_sp"]
)
schema2_columns = set(
["Date", "close_tsla", "volume_tsla", "open_tsla", "high_tsla", "low_tsla"]
)
# Identify dataframes based on columns
if set(dataframes[0].columns) == schema1_columns:
df_sp = dataframes[0]
df_tsla = dataframes[1]
elif set(dataframes[0].columns) == schema2_columns:
df_sp = dataframes[1]
df_tsla = dataframes[0]
else:
return (
empty_div(),
empty_dataframe_list(),
html.Div(),
)
# Convert date columns (for safety, in case they're not in datetime format)
df_sp["Date"] = pd.to_datetime(df_sp["Date"])
df_tsla["Date"] = pd.to_datetime(df_tsla["Date"])
# Merge using an outer join on the Date column
merged_df = df_sp.merge(df_tsla, on="Date", how="outer")
cols_to_convert = [col for col in merged_df.columns if col != "Date"]
merged_df[cols_to_convert] = merged_df[cols_to_convert].astype(float)
merged_df_list = [merged_df.columns.tolist()] + merged_df.values.tolist()
if not merged_df.empty:
# Time Series Plot for Closing Prices
fig1 = px.line(
merged_df,
x="Date",
y="close_sp",
labels={"close_sp": "S&P Close Price"},
title="Close Price Comparison",
)
fig1.add_scatter(
x=merged_df["Date"],
y=merged_df["close_tsla"],
mode="lines",
name="TSLA Close Price",
yaxis="y2",
)
fig1.update_layout(
yaxis=dict(title="S&P Close Price"),
yaxis2=dict(title="TSLA Close Price", overlaying="y", side="right"),
)
# Volume Bar Chart
fig2 = px.line(
merged_df,
x="Date",
y="volume_sp",
labels={"volume_sp": "S&P Volume"},
title="Trading Volume Comparison",
)
# Add TSLA volume with secondary y-axis
fig2.add_scatter(
x=merged_df["Date"],
y=merged_df["volume_tsla"],
mode="lines",
name="TSLA Volume",
yaxis="y2",
)
# Update layout to specify y-axis properties
fig2.update_layout(
yaxis=dict(title="S&P Volume"),
yaxis2=dict(title="TSLA Volume", overlaying="y", side="right"),
)
# Moving Average Plot
merged_df["S&P_MA30"] = merged_df["close_sp"].rolling(window=30).mean()
merged_df["TSLA_MA30"] = merged_df["close_tsla"].rolling(window=30).mean()
fig3 = px.line(
merged_df,
x="Date",
y="S&P_MA30",
labels={"S&P_MA30": "S&P 30-Day MA"},
title="30-Day Moving Average Comparison",
)
fig3.add_scatter(
x=merged_df["Date"],
y=merged_df["TSLA_MA30"],
mode="lines",
name="TSLA 30-Day MA",
yaxis="y2",
)
fig3.update_layout(
yaxis=dict(title="S&P 30-Day Moving Average"),
yaxis2=dict(
title="TSLA 30-Day Moving Average", overlaying="y", side="right"
),
)
# Compute the correlation coefficients
correlations = {
"Metric": ["Open", "Close", "Volume"],
"Pearson Correlation": [
merged_df["open_sp"].corr(merged_df["open_tsla"]),
merged_df["close_sp"].corr(merged_df["close_tsla"]),
merged_df["volume_sp"].corr(merged_df["volume_tsla"]),
],
}
correlations_df = pd.DataFrame(correlations)
layout = [
dash_table.DataTable(
data=correlations_df.to_dict("records"),
style_cell={"textAlign": "center"},
),
dmc.Group(
children=[dcc.Graph(figure=fig1), dcc.Graph(figure=fig2)],
position="center",
grow=True,
),
dcc.Graph(figure=fig3),
]
else:
layout = []
return (
layout,
merged_df_list,
html.Div( # Add a container for the section below the pivot table
className="data-table-container",
children=[
# Here you can add elements for the data table and related items
html.H3(
"Data Analysis",
style={
"text-align": "center",
"margin-top": "20px",
"color": "#333",
"width": "100%",
},
),
# Here is where you'd add the 'data_table' and any other elements
# Example:
# dash_table.DataTable(id='data_table')
],
style={
"margin-top": "20px",
"padding": "10px",
"background-color": "#f9f9f9",
"box-shadow": "0px 2px 5px rgba(0, 0, 0, 0.1)",
"border-radius": "5px",
"width": "100%",
},
),
)
if __name__ == "__main__":
app.run_server(debug=True)