This page provides the UI for users to upload data and create new analyses.
# new_recipe_page.py
import streamlit as st
from gws_core import (
ProcessProxy, ScenarioProxy, ProtocolProxy,
ScenarioCreationType, InputTask, ResourceModel
)
from gws_core.streamlit import StreamlitRouter, StreamlitResourceSelect
from .my_custom_state import MyCustomState
from ..my_custom_load_data.my_custom_load_data import MyCustomLoadData
def render_new_recipe_page(custom_state: MyCustomState) -> None:
"""Render the new analysis creation page."""
translate_service = custom_state.get_translate_service()
# Back button
router = StreamlitRouter.load_from_session()
if st.button("← Return to recipes"):
router.navigate("first-page")
st.markdown("## 🧬 New Custom Analysis")
# Recipe name
st.subheader("📝 Recipe Details")
analysis_name = st.text_input(
"Recipe Name",
key="analysis_name_input",
placeholder="Enter recipe name..."
)
# File upload section
st.subheader("📁 Upload Data Files")
file_mode = st.radio(
"Input Mode",
options=['upload', 'select_existing'],
format_func=lambda x: "Upload Files" if x == 'upload' else "Select Existing Resources",
horizontal=True
)
if file_mode == 'upload':
# File uploaders
col1, col2, col3 = st.columns(3)
with col1:
st.write("**1. Info File**")
info_file = st.file_uploader("Select Info CSV", type=['csv'])
with col2:
st.write("**2. Medium File**")
medium_file = st.file_uploader("Select Medium CSV", type=['csv'])
with col3:
st.write("**3. Time Series Data**")
timeseries_file = st.file_uploader("Select Time Series ZIP", type=['zip'])
# Launch button
if st.button("🚀 Launch Analysis", type="primary", use_container_width=True):
if not all([analysis_name, info_file, medium_file, timeseries_file]):
st.error("⚠️ Please provide recipe name and all required files")
else:
# Create scenario with uploaded files
launch_scenario_with_uploads(
custom_state, analysis_name,
info_file, medium_file, timeseries_file
)
else:
# Resource selection mode
st.info("Select existing resources from your workspace")
info_resource = StreamlitResourceSelect.select_single_resource(
resource_type=File,
label="Info CSV Resource",
key="info_resource_select"
)
medium_resource = StreamlitResourceSelect.select_single_resource(
resource_type=File,
label="Medium CSV Resource",
key="medium_resource_select"
)
timeseries_resource = StreamlitResourceSelect.select_single_resource(
resource_type=Folder,
label="Time Series Folder Resource",
key="timeseries_resource_select"
)
if st.button("🚀 Launch Analysis", type="primary"):
if not all([analysis_name, info_resource, medium_resource, timeseries_resource]):
st.error("⚠️ Please provide recipe name and select all resources")
else:
launch_scenario_with_resources(
custom_state, analysis_name,
info_resource, medium_resource, timeseries_resource
)
def launch_scenario_with_uploads(state, recipe_name, info_file, medium_file, timeseries_file):
"""Launch a scenario with uploaded files."""
try:
# Create protocol with Load Data Task
process = ProcessProxy(MyCustomLoadData)
# Configure inputs
process.set_param('recipe_name', recipe_name)
process.set_param('pipeline_id', f"pipeline_{recipe_name}_{datetime.now().timestamp()}")
# Set input files
process.set_input('info_csv', InputTask.from_uploaded_file(info_file))
process.set_input('medium_csv', InputTask.from_uploaded_file(medium_file))
process.set_input('timeseries_folder', InputTask.from_uploaded_file(timeseries_file))
# Create protocol
protocol = ProtocolProxy()
protocol.add_process(process, 'load_data')
# Create scenario
scenario_proxy = ScenarioProxy()
scenario_proxy.set_protocol(protocol)
scenario_proxy.set_creation_type(ScenarioCreationType.MANUAL)
# Add tags
scenario_proxy.add_tag('my_custom_app', 'true')
scenario_proxy.add_tag('my_custom_recipe_name', recipe_name)
# Save and run
scenario = scenario_proxy.save()
scenario.run()
st.success(f"✅ Analysis '{recipe_name}' launched! ID: {scenario.id}")
# Navigate to recipe page
router = StreamlitRouter.load_from_session()
router.navigate("analysis", query_params={'scenario_id': scenario.id})
except Exception as e:
st.error(f"❌ Error launching analysis: {str(e)}")
def launch_scenario_with_resources(state, recipe_name, info_res, medium_res, timeseries_res):
"""Launch scenario with existing resources."""
# Similar to above, but use ResourceModel instead of uploaded files
pass