import streamlit as st
import plotly.graph_objects as go
from plotly.subplots import make_subplots
import time
from mock_data.sample_data import generate_optimization_progress
def render_progress_chart(budget_hours=2, is_running=False, enable_animation=True, current_step=None):
"""Render the optimization progress chart with dual axes.
Args:
budget_hours: Estimated hours for optimization
is_running: Whether optimization is currently running
enable_animation: If True, uses 10-second animation with sleep (for local demos)
If False, instant updates (for Hugging Face Spaces)
current_step: Current step in the optimization (1-20), None to use session state
"""
# Get mock data based on budget hours
df = generate_optimization_progress(budget_hours)
total_rows = len(df) # Store original length before slicing
# If optimization is "running", only show partial data
if is_running:
# Use provided current_step or get from session state
if current_step is None:
current_step = st.session_state.get('progress_step', 1)
# Always show at least 1 row so chart is visible from the start
display_rows = max(1, min(current_step, total_rows))
df = df.iloc[:display_rows]
else:
# Show all data when not running
if 'progress_step' in st.session_state:
del st.session_state.progress_step
if 'last_update' in st.session_state:
del st.session_state.last_update
# Create figure with secondary y-axis
fig = make_subplots(specs=[[{"secondary_y": True}]])
# Add accuracy trace with improved styling
fig.add_trace(
go.Scatter(
x=df['search_time'],
y=df['accuracy'],
name='Accuracy',
mode='lines+markers',
line=dict(color='#636EFA', width=3),
marker=dict(size=8, symbol='circle'),
fill='tozeroy',
fillcolor='rgba(99, 110, 250, 0.1)',
hovertemplate='Time: %{x:.2f}h
' +
'Accuracy: %{y:.1f}%
' +
'
' +
'Size: %{y:.1f} GB
' +
'