Spaces:
Running
Running
import streamlit as st | |
import pandas as pd | |
import plotly.express as px | |
# Set the page layout for Streamlit | |
st.set_page_config(layout="wide") | |
# Title and Introduction | |
st.title("Data Visualization App") | |
st.markdown(""" | |
This app allows you to upload a table (CSV or Excel) and ask for graphs to visualize the data. | |
Based on your question, the app will generate interactive graphs using **Plotly**. | |
### Available Features: | |
- **Graph of a column**: Visualize a column as a graph. | |
- **Scatter plot**: Visualize relationships between two columns. | |
Upload your data and ask questions to generate visualizations. | |
""") | |
# File uploader in the sidebar | |
file_name = st.sidebar.file_uploader("Upload file:", type=['csv', 'xlsx']) | |
# File processing and question answering | |
if file_name is None: | |
st.markdown('<p class="font">Please upload an excel or csv file </p>', unsafe_allow_html=True) | |
else: | |
try: | |
# Check file type and handle reading accordingly | |
if file_name.name.endswith('.csv'): | |
df = pd.read_csv(file_name, sep=';', encoding='ISO-8859-1') # Adjust encoding if needed | |
elif file_name.name.endswith('.xlsx'): | |
df = pd.read_excel(file_name, engine='openpyxl') # Use openpyxl to read .xlsx files | |
else: | |
st.error("Unsupported file type") | |
df = None | |
if df is not None: | |
numeric_columns = df.select_dtypes(include=['object']).columns | |
for col in numeric_columns: | |
df[col] = pd.to_numeric(df[col], errors='ignore') | |
st.write("Original Data:") | |
st.write(df) | |
df_numeric = df.copy() | |
df = df.astype(str) | |
# Display the first 5 rows of the dataframe in an editable grid | |
st.write("Sample data for graph generation:") | |
st.write(df.head()) | |
except Exception as e: | |
st.error(f"Error reading file: {str(e)}") | |
# User input for the graph query | |
question = st.text_input('Ask your graph-related question') | |
with st.spinner(): | |
if st.button('Generate Graph'): | |
try: | |
if 'between' in question.lower() and 'and' in question.lower(): | |
# Handle scatter plot (graph between two columns) | |
columns = question.split('between')[-1].split('and') | |
columns = [col.strip() for col in columns] | |
if len(columns) == 2 and all(col in df.columns for col in columns): | |
fig = px.scatter(df, x=columns[0], y=columns[1], title=f"Scatter Plot between {columns[0]} and {columns[1]}") | |
st.plotly_chart(fig, use_container_width=True) | |
st.success(f"Here is the scatter plot between '{columns[0]}' and '{columns[1]}'.") | |
else: | |
st.warning("Columns not found in the dataset.") | |
elif 'column' in question.lower(): | |
# Handle graph of a single column | |
column = question.split('of')[-1].strip() | |
if column in df.columns: | |
fig = px.line(df, x=df.index, y=column, title=f"Graph of column '{column}'") | |
st.plotly_chart(fig, use_container_width=True) | |
st.success(f"Here is the graph of column '{column}'.") | |
else: | |
st.warning(f"Column '{column}' not found in the data.") | |
else: | |
st.warning("Please ask a valid graph-related question (e.g., 'make a graph between column1 and column2').") | |
except Exception as e: | |
st.warning(f"Error processing question or generating graph: {str(e)}") | |