Spaces:
Sleeping
Sleeping
Update src/streamlit_app.py
Browse files- src/streamlit_app.py +65 -38
src/streamlit_app.py
CHANGED
@@ -1,40 +1,67 @@
|
|
1 |
-
|
2 |
-
import numpy as np
|
3 |
-
import pandas as pd
|
4 |
import streamlit as st
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
5 |
|
6 |
-
|
7 |
-
|
8 |
-
|
9 |
-
|
10 |
-
|
11 |
-
|
12 |
-
|
13 |
-
In the meantime, below is an example of what you can do with just a few lines of code:
|
14 |
-
"""
|
15 |
-
|
16 |
-
num_points = st.slider("Number of points in spiral", 1, 10000, 1100)
|
17 |
-
num_turns = st.slider("Number of turns in spiral", 1, 300, 31)
|
18 |
-
|
19 |
-
indices = np.linspace(0, 1, num_points)
|
20 |
-
theta = 2 * np.pi * num_turns * indices
|
21 |
-
radius = indices
|
22 |
-
|
23 |
-
x = radius * np.cos(theta)
|
24 |
-
y = radius * np.sin(theta)
|
25 |
-
|
26 |
-
df = pd.DataFrame({
|
27 |
-
"x": x,
|
28 |
-
"y": y,
|
29 |
-
"idx": indices,
|
30 |
-
"rand": np.random.randn(num_points),
|
31 |
-
})
|
32 |
-
|
33 |
-
st.altair_chart(alt.Chart(df, height=700, width=700)
|
34 |
-
.mark_point(filled=True)
|
35 |
-
.encode(
|
36 |
-
x=alt.X("x", axis=None),
|
37 |
-
y=alt.Y("y", axis=None),
|
38 |
-
color=alt.Color("idx", legend=None, scale=alt.Scale()),
|
39 |
-
size=alt.Size("rand", legend=None, scale=alt.Scale(range=[1, 150])),
|
40 |
-
))
|
|
|
1 |
+
# File: streamlit_dashboard.py
|
|
|
|
|
2 |
import streamlit as st
|
3 |
+
import requests
|
4 |
+
import pandas as pd
|
5 |
+
import plotly.express as px
|
6 |
+
|
7 |
+
BASE_URL = "https://splashdatavisualize-production.up.railway.app/api/visualizations"
|
8 |
+
|
9 |
+
st.title("📊 Sales Visualization Dashboard")
|
10 |
+
st.markdown("Use the sidebar to choose a visualization.")
|
11 |
+
|
12 |
+
chart_type = st.sidebar.selectbox("Select a Chart", [
|
13 |
+
"Sales by Country",
|
14 |
+
"Sales by Category",
|
15 |
+
"Share by Country",
|
16 |
+
"Share by Category",
|
17 |
+
"Geo Sales",
|
18 |
+
"Grouped Country & Category",
|
19 |
+
"Stacked Country & Category"
|
20 |
+
])
|
21 |
+
|
22 |
+
# 1. Sales by Country
|
23 |
+
if chart_type == "Sales by Country":
|
24 |
+
data = requests.get(f"{BASE_URL}/sales-by-country").json()
|
25 |
+
df = pd.DataFrame(data.items(), columns=["Country", "Sales"])
|
26 |
+
st.plotly_chart(px.bar(df, x="Country", y="Sales", title="Sales by Country"))
|
27 |
+
|
28 |
+
# 2. Sales by Category
|
29 |
+
elif chart_type == "Sales by Category":
|
30 |
+
data = requests.get(f"{BASE_URL}/sales-by-category").json()
|
31 |
+
df = pd.DataFrame(data.items(), columns=["Category", "Sales"])
|
32 |
+
st.plotly_chart(px.bar(df, x="Category", y="Sales", title="Sales by Category"))
|
33 |
+
|
34 |
+
# 3. Share by Country (Pie Chart)
|
35 |
+
elif chart_type == "Share by Country":
|
36 |
+
data = requests.get(f"{BASE_URL}/share-by-country").json()
|
37 |
+
df = pd.DataFrame(data.items(), columns=["Country", "Share %"])
|
38 |
+
st.plotly_chart(px.pie(df, names="Country", values="Share %", title="Share by Country"))
|
39 |
+
|
40 |
+
# 4. Share by Category (Pie Chart)
|
41 |
+
elif chart_type == "Share by Category":
|
42 |
+
data = requests.get(f"{BASE_URL}/share-by-category").json()
|
43 |
+
df = pd.DataFrame(data.items(), columns=["Category", "Share %"])
|
44 |
+
st.plotly_chart(px.pie(df, names="Category", values="Share %", title="Share by Category"))
|
45 |
+
|
46 |
+
# 5. Geo Sales
|
47 |
+
elif chart_type == "Geo Sales":
|
48 |
+
data = requests.get(f"{BASE_URL}/geo-sales").json()
|
49 |
+
df = pd.DataFrame(data)
|
50 |
+
st.plotly_chart(px.choropleth(df, locations="country", locationmode="country names",
|
51 |
+
color="value", title="Geo Sales by Country", color_continuous_scale="Blues"))
|
52 |
+
|
53 |
+
# 6. Grouped Country & Category
|
54 |
+
elif chart_type == "Grouped Country & Category":
|
55 |
+
data = requests.get(f"{BASE_URL}/grouped-country-category").json()
|
56 |
+
df = pd.DataFrame(data).T.reset_index().rename(columns={"index": "Country"})
|
57 |
+
df = df.melt(id_vars="Country", var_name="Category", value_name="Sales")
|
58 |
+
st.plotly_chart(px.bar(df, x="Country", y="Sales", color="Category", barmode="group",
|
59 |
+
title="Grouped Sales by Country and Category"))
|
60 |
|
61 |
+
# 7. Stacked Country & Category
|
62 |
+
elif chart_type == "Stacked Country & Category":
|
63 |
+
data = requests.get(f"{BASE_URL}/stacked-country-category").json()
|
64 |
+
df = pd.DataFrame(data)
|
65 |
+
df = df.melt(id_vars="country", var_name="Category", value_name="Sales")
|
66 |
+
st.plotly_chart(px.bar(df, x="country", y="Sales", color="Category", barmode="stack",
|
67 |
+
title="Stacked Sales by Country and Category"))
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|