From 16d2ae6f60218d6d9964be57bc47362de1efd828 Mon Sep 17 00:00:00 2001 From: Ben Vincent Date: Sun, 9 Mar 2025 16:48:42 +1100 Subject: [PATCH] first commit --- .gitignore | 1 + Makefile | 12 +++++++++++ app.py | 54 ++++++++++++++++++++++++++++++++++++++++++++++++ requirements.txt | 4 ++++ 4 files changed, 71 insertions(+) create mode 100644 .gitignore create mode 100644 Makefile create mode 100644 app.py create mode 100644 requirements.txt diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..1d17dae --- /dev/null +++ b/.gitignore @@ -0,0 +1 @@ +.venv diff --git a/Makefile b/Makefile new file mode 100644 index 0000000..e45f808 --- /dev/null +++ b/Makefile @@ -0,0 +1,12 @@ +PYTHON := 3.12 +VENV_PATH := .venv +PORT := 8502 + +.PHONY: venv + +venv: + uv venv --python $(PYTHON) --allow-existing $(VENV_PATH) + source $(VENV_PATH)/bin/activate && uv pip install -r requirements.txt + +run: venv + source $(VENV_PATH)/bin/activate && $(VENV_PATH)/bin/streamlit run app.py --server.port $(PORT) diff --git a/app.py b/app.py new file mode 100644 index 0000000..5cd4083 --- /dev/null +++ b/app.py @@ -0,0 +1,54 @@ +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 diff --git a/requirements.txt b/requirements.txt new file mode 100644 index 0000000..4335efe --- /dev/null +++ b/requirements.txt @@ -0,0 +1,4 @@ +matplotlib==3.10.1 +pandas==2.2.3 +plotly==6.0.0 +streamlit==1.43.1