Spaces:
Sleeping
Sleeping
Create app.py
Browse files
app.py
ADDED
@@ -0,0 +1,83 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import pandas as pd
|
2 |
+
import requests
|
3 |
+
import matplotlib.font_manager as fm
|
4 |
+
import matplotlib as mpl
|
5 |
+
import streamlit as st
|
6 |
+
|
7 |
+
# Download and save the first CSV file
|
8 |
+
url1 = "https://mopsfin.twse.com.tw/opendata/t187ap4"
|
9 |
+
response1 = requests.get(url1)
|
10 |
+
with open("20240627.csv", "wb") as file1:
|
11 |
+
file1.write(response1.content)
|
12 |
+
df1 = pd.read_csv("20240627.csv")
|
13 |
+
|
14 |
+
# Download and save the second CSV file
|
15 |
+
url2 = "https://mopsfin.twse.com.tw/opendata/t187ap46_O_2.csv"
|
16 |
+
response2 = requests.get(url2)
|
17 |
+
with open("20240628.csv", "wb") as file2:
|
18 |
+
file2.write(response2.content)
|
19 |
+
df2 = pd.read_csv("20240628.csv")
|
20 |
+
|
21 |
+
# Download and save the third CSV file
|
22 |
+
url3 = "https://mopsfin.twse.com.tw/opendata/t187ap46_L_6.csv"
|
23 |
+
response3 = requests.get(url3)
|
24 |
+
with open("20240629.csv", "wb") as file3:
|
25 |
+
file3.write(response3.content)
|
26 |
+
df3 = pd.read_csv("20240629.csv")
|
27 |
+
|
28 |
+
# Fill NaN values with 0
|
29 |
+
df1.fillna(0, inplace=True)
|
30 |
+
df2.fillna(0, inplace=True)
|
31 |
+
df3.fillna(0, inplace=True)
|
32 |
+
|
33 |
+
# Download and set custom font
|
34 |
+
font_url = "https://drive.google.com/uc?id=1eGAsTN1HBpJAkeVM57_C7ccp7hbgSz3_&export=download"
|
35 |
+
font_response = requests.get(font_url)
|
36 |
+
with open("TaipeiSansTCBeta-Regular.ttf", "wb") as font_file:
|
37 |
+
font_file.write(font_response.content)
|
38 |
+
|
39 |
+
fm.fontManager.addfont("TaipeiSansTCBeta-Regular.ttf")
|
40 |
+
mpl.rc('font', family='Taipei Sans TC Beta')
|
41 |
+
|
42 |
+
# Streamlit app setup
|
43 |
+
st.title("Taiwan Stock Exchange Financial Data Analysis")
|
44 |
+
|
45 |
+
# Dropdown to select which DataFrame to display
|
46 |
+
df_selection = st.selectbox("Select DataFrame to View", ("20240627", "20240628", "20240629"))
|
47 |
+
|
48 |
+
if df_selection == "20240627":
|
49 |
+
df = df1
|
50 |
+
elif df_selection == "20240628":
|
51 |
+
df = df2
|
52 |
+
else:
|
53 |
+
df = df3
|
54 |
+
|
55 |
+
st.write(f"Showing data for {df_selection}")
|
56 |
+
st.write(df)
|
57 |
+
|
58 |
+
# Dynamic plot options
|
59 |
+
chart_type = st.selectbox("Select Chart Type", ("Line Chart", "Bar Chart"))
|
60 |
+
|
61 |
+
if chart_type == "Line Chart":
|
62 |
+
st.line_chart(df)
|
63 |
+
elif chart_type == "Bar Chart":
|
64 |
+
st.bar_chart(df)
|
65 |
+
|
66 |
+
# Optionally, more advanced plots using matplotlib
|
67 |
+
import matplotlib.pyplot as plt
|
68 |
+
|
69 |
+
st.write("Custom Plot")
|
70 |
+
|
71 |
+
fig, ax = plt.subplots()
|
72 |
+
ax.plot(df.index, df.iloc[:, 0], label="Column 1")
|
73 |
+
ax.plot(df.index, df.iloc[:, 1], label="Column 2")
|
74 |
+
ax.set_xlabel("Index")
|
75 |
+
ax.set_ylabel("Values")
|
76 |
+
ax.legend()
|
77 |
+
st.pyplot(fig)
|
78 |
+
|
79 |
+
# Save the plot if needed
|
80 |
+
save_plot = st.button("Save Plot as Image")
|
81 |
+
if save_plot:
|
82 |
+
fig.savefig("custom_plot.png")
|
83 |
+
st.write("Plot saved as custom_plot.png")
|