Spaces:
Sleeping
Sleeping
Create app1.py
Browse files
app1.py
ADDED
|
@@ -0,0 +1,85 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import streamlit as st
|
| 2 |
+
import pandas as pd
|
| 3 |
+
import plotly.express as px
|
| 4 |
+
|
| 5 |
+
# Set the page layout for Streamlit
|
| 6 |
+
st.set_page_config(layout="wide")
|
| 7 |
+
|
| 8 |
+
# Title and Introduction
|
| 9 |
+
st.title("Data Visualization App")
|
| 10 |
+
st.markdown("""
|
| 11 |
+
This app allows you to upload a table (CSV or Excel) and ask for graphs to visualize the data.
|
| 12 |
+
Based on your question, the app will generate interactive graphs using **Plotly**.
|
| 13 |
+
|
| 14 |
+
### Available Features:
|
| 15 |
+
- **Graph of a column**: Visualize a column as a graph.
|
| 16 |
+
- **Scatter plot**: Visualize relationships between two columns.
|
| 17 |
+
|
| 18 |
+
Upload your data and ask questions to generate visualizations.
|
| 19 |
+
""")
|
| 20 |
+
|
| 21 |
+
# File uploader in the sidebar
|
| 22 |
+
file_name = st.sidebar.file_uploader("Upload file:", type=['csv', 'xlsx'])
|
| 23 |
+
|
| 24 |
+
# File processing and question answering
|
| 25 |
+
if file_name is None:
|
| 26 |
+
st.markdown('<p class="font">Please upload an excel or csv file </p>', unsafe_allow_html=True)
|
| 27 |
+
else:
|
| 28 |
+
try:
|
| 29 |
+
# Check file type and handle reading accordingly
|
| 30 |
+
if file_name.name.endswith('.csv'):
|
| 31 |
+
df = pd.read_csv(file_name, sep=';', encoding='ISO-8859-1') # Adjust encoding if needed
|
| 32 |
+
elif file_name.name.endswith('.xlsx'):
|
| 33 |
+
df = pd.read_excel(file_name, engine='openpyxl') # Use openpyxl to read .xlsx files
|
| 34 |
+
else:
|
| 35 |
+
st.error("Unsupported file type")
|
| 36 |
+
df = None
|
| 37 |
+
|
| 38 |
+
if df is not None:
|
| 39 |
+
numeric_columns = df.select_dtypes(include=['object']).columns
|
| 40 |
+
for col in numeric_columns:
|
| 41 |
+
df[col] = pd.to_numeric(df[col], errors='ignore')
|
| 42 |
+
|
| 43 |
+
st.write("Original Data:")
|
| 44 |
+
st.write(df)
|
| 45 |
+
|
| 46 |
+
df_numeric = df.copy()
|
| 47 |
+
df = df.astype(str)
|
| 48 |
+
|
| 49 |
+
# Display the first 5 rows of the dataframe in an editable grid
|
| 50 |
+
st.write("Sample data for graph generation:")
|
| 51 |
+
st.write(df.head())
|
| 52 |
+
|
| 53 |
+
except Exception as e:
|
| 54 |
+
st.error(f"Error reading file: {str(e)}")
|
| 55 |
+
|
| 56 |
+
# User input for the graph query
|
| 57 |
+
question = st.text_input('Ask your graph-related question')
|
| 58 |
+
|
| 59 |
+
with st.spinner():
|
| 60 |
+
if st.button('Generate Graph'):
|
| 61 |
+
try:
|
| 62 |
+
if 'between' in question.lower() and 'and' in question.lower():
|
| 63 |
+
# Handle scatter plot (graph between two columns)
|
| 64 |
+
columns = question.split('between')[-1].split('and')
|
| 65 |
+
columns = [col.strip() for col in columns]
|
| 66 |
+
if len(columns) == 2 and all(col in df.columns for col in columns):
|
| 67 |
+
fig = px.scatter(df, x=columns[0], y=columns[1], title=f"Scatter Plot between {columns[0]} and {columns[1]}")
|
| 68 |
+
st.plotly_chart(fig, use_container_width=True)
|
| 69 |
+
st.success(f"Here is the scatter plot between '{columns[0]}' and '{columns[1]}'.")
|
| 70 |
+
else:
|
| 71 |
+
st.warning("Columns not found in the dataset.")
|
| 72 |
+
elif 'column' in question.lower():
|
| 73 |
+
# Handle graph of a single column
|
| 74 |
+
column = question.split('of')[-1].strip()
|
| 75 |
+
if column in df.columns:
|
| 76 |
+
fig = px.line(df, x=df.index, y=column, title=f"Graph of column '{column}'")
|
| 77 |
+
st.plotly_chart(fig, use_container_width=True)
|
| 78 |
+
st.success(f"Here is the graph of column '{column}'.")
|
| 79 |
+
else:
|
| 80 |
+
st.warning(f"Column '{column}' not found in the data.")
|
| 81 |
+
else:
|
| 82 |
+
st.warning("Please ask a valid graph-related question (e.g., 'make a graph between column1 and column2').")
|
| 83 |
+
|
| 84 |
+
except Exception as e:
|
| 85 |
+
st.warning(f"Error processing question or generating graph: {str(e)}")
|