akazmi commited on
Commit
001a9c1
·
verified ·
1 Parent(s): 9cbb15c

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +29 -63
app.py CHANGED
@@ -1,83 +1,49 @@
1
  import streamlit as st
2
  import pandas as pd
3
- import plotly.graph_objects as go
4
 
5
  # Title of the App
6
- st.title("Excel Data Visualization with Dual Y-Axis")
7
 
8
- # Upload Section
9
  uploaded_file = st.file_uploader("Upload your Excel file", type=["xlsx", "xls"])
10
 
11
  if uploaded_file:
12
  try:
13
- # Read the Excel file into a DataFrame
14
  df = pd.read_excel(uploaded_file)
15
-
16
- # Preview the uploaded data
17
  st.write("Data Preview:")
18
  st.dataframe(df)
19
 
20
- # Ensure required columns are present
21
- required_columns = ["Description", "Oct'24", "Nov'24", "Dec/(Inc)"]
22
- if not all(column in df.columns for column in required_columns):
23
- st.error(f"The uploaded file must contain the following columns: {', '.join(required_columns)}")
24
- else:
25
- # Plot the Graph
26
- fig = go.Figure()
27
-
28
- # Add Bar Traces for Oct'24 and Nov'24
29
- fig.add_trace(go.Bar(
30
- x=df["Description"],
31
- y=df["Oct'24"],
32
- name="Oct'24",
33
- marker_color='blue'
34
- ))
35
-
36
- fig.add_trace(go.Bar(
37
- x=df["Description"],
38
- y=df["Nov'24"],
39
- name="Nov'24",
40
- marker_color='green'
41
- ))
42
-
43
- # Add Line Trace for Dec/(Inc) on Secondary Y-axis
44
- fig.add_trace(go.Scatter(
45
- x=df["Description"],
46
- y=df["Dec/(Inc)"],
47
- name="Dec/(Inc)",
48
- mode="lines+markers",
49
- marker=dict(color='red'),
50
- yaxis="y2"
51
- ))
52
-
53
- # Update Layout for Dual Y-axis
54
- fig.update_layout(
55
- title="Dual Axis Chart",
56
- xaxis=dict(title="Description"),
57
- yaxis=dict(
58
- title="Values (Oct'24 and Nov'24)",
59
- titlefont=dict(color="blue"),
60
- tickfont=dict(color="blue"),
61
- ),
62
- yaxis2=dict(
63
- title="Dec/(Inc)",
64
- titlefont=dict(color="red"),
65
- tickfont=dict(color="red"),
66
- overlaying="y",
67
- side="right"
68
- ),
69
- barmode="group",
70
- legend=dict(x=0.5, y=1.2, orientation="h"),
71
- template="plotly_white"
72
- )
73
-
74
- # Render the Plot
75
  st.plotly_chart(fig)
 
 
76
  except Exception as e:
77
  st.error(f"Error processing the file: {e}")
78
  else:
79
- st.info("Please upload an Excel file to get started.")
80
 
81
  # Footer
82
  st.write("---")
83
- st.write("Developed using [Streamlit](https://streamlit.io) and [Hugging Face Spaces](https://huggingface.co/spaces).")
 
1
  import streamlit as st
2
  import pandas as pd
3
+ import plotly.express as px
4
 
5
  # Title of the App
6
+ st.title("Smart Data Visualization App")
7
 
8
+ # File Upload Section
9
  uploaded_file = st.file_uploader("Upload your Excel file", type=["xlsx", "xls"])
10
 
11
  if uploaded_file:
12
  try:
13
+ # Load the Excel file into a DataFrame
14
  df = pd.read_excel(uploaded_file)
 
 
15
  st.write("Data Preview:")
16
  st.dataframe(df)
17
 
18
+ # User selects columns to visualize
19
+ x_axis = st.selectbox("Select X-axis column", df.columns)
20
+ y_axis = st.multiselect("Select Y-axis column(s)", [col for col in df.columns if col != x_axis])
21
+
22
+ if x_axis and y_axis:
23
+ # Determine the best type of graph
24
+ if df[x_axis].nunique() < len(df[x_axis]) * 0.5:
25
+ # If the X-axis is categorical, choose a bar chart
26
+ chart_type = "Bar Chart"
27
+ fig = px.bar(df, x=x_axis, y=y_axis, barmode="group")
28
+ elif len(y_axis) == 1:
29
+ # For single numerical column, plot a line chart
30
+ chart_type = "Line Chart"
31
+ fig = px.line(df, x=x_axis, y=y_axis[0])
32
+ else:
33
+ # If multiple numerical columns are selected, use a scatter plot matrix
34
+ chart_type = "Scatter Matrix"
35
+ fig = px.scatter_matrix(df, dimensions=y_axis, color=x_axis)
36
+
37
+ # Display the selected chart type and plot
38
+ st.write(f"Automatically Selected Chart Type: {chart_type}")
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
39
  st.plotly_chart(fig)
40
+ else:
41
+ st.info("Please select columns for the X and Y axes to visualize.")
42
  except Exception as e:
43
  st.error(f"Error processing the file: {e}")
44
  else:
45
+ st.info("Upload an Excel file to get started.")
46
 
47
  # Footer
48
  st.write("---")
49
+ st.write("Powered by [Streamlit](https://streamlit.io) and [Plotly](https://plotly.com).")