app-sudaporn-research-indiv.../app.py
2025-03-09 16:48:42 +11:00

55 lines
1.8 KiB
Python

import streamlit as st
import pandas as pd
import plotly.graph_objects as go
# Function to process and plot CSV data
def process_and_plot_cumulative_csv(df):
"""Creates an interactive plot for each book in the dataset."""
# Extract book names and cumulative chapter values
book_names = df.iloc[:, 0] # First column contains book names
cumulative_books = df.iloc[:, 2:].apply(pd.to_numeric, errors='coerce').values # Convert chapter values to numeric
# Display each book separately
for i, book in enumerate(cumulative_books):
book = book[~pd.isna(book)] # Remove NaN values
# Create an interactive Plotly figure
fig = go.Figure()
fig.add_trace(go.Scatter(
x=list(range(1, len(book) + 1)),
y=book,
mode='lines+markers',
name=book_names[i],
marker=dict(size=6),
hoverinfo="x+y+name"
))
# Set layout for the plot
fig.update_layout(
title=f'{book_names[i]}',
xaxis_title="Chapter Index",
yaxis_title="Cumulative Change",
legend_title="Book",
hovermode="x unified",
template="plotly_white"
)
# Display the interactive plot in Streamlit
st.plotly_chart(fig)
# Streamlit UI
st.title("📖 Book Analysis: Individual Cumulative Chapter Changes (Interactive)")
# File uploader
uploaded_file = st.file_uploader("📂 Upload CSV File", type=["csv"])
if uploaded_file is not None:
df = pd.read_csv(uploaded_file) # Read uploaded file
st.write("### 📊 Data Preview:")
st.dataframe(df) # Show uploaded data
st.write("### 📈 Interactive Plots for Each Book:")
process_and_plot_cumulative_csv(df) # Generate interactive plots