import streamlit as st import streamlit.components.v1 as components # Initialize session state if 'settings' not in st.session_state: st.session_state['settings'] = {} # Function to update widgets based on saved settings def load_settings(): settings = st.session_state['settings'] st.session_state.cube_color = settings.get("cube_color", "#00ff00") st.session_state.rotation_speed = settings.get("rotation_speed", 0.01) st.session_state.cube_position_x = settings.get("cube_position_x", 0) st.session_state.cube_position_y = settings.get("cube_position_y", 0) st.session_state.cube_position_z = settings.get("cube_position_z", 0) st.session_state.cube_scale = settings.get("cube_scale", 1.0) st.session_state.show_bounding_box = settings.get("show_bounding_box", True) st.session_state.spotlight_intensity = settings.get("spotlight_intensity", 1.0) st.session_state.ambient_light_intensity = settings.get("ambient_light_intensity", 0.5) st.session_state.camera_view = settings.get("camera_view", "Default") st.write("Loaded settings into session state:", settings) # Create two columns for layout col1, col2 = st.columns([1, 3]) with col1: # Streamlit widgets to control the scene cube_color = st.color_picker("Pick a cube color", st.session_state.get('cube_color', "#00ff00")) rotation_speed = st.slider("Rotation Speed", min_value=0.01, max_value=0.1, value=st.session_state.get('rotation_speed', 0.01)) cube_position_x = st.slider("Cube Position X", min_value=-10, max_value=10, value=st.session_state.get('cube_position_x', 0)) cube_position_y = st.slider("Cube Position Y", min_value=-10, max_value=10, value=st.session_state.get('cube_position_y', 0)) cube_position_z = st.slider("Cube Position Z", min_value=-10, max_value=10, value=st.session_state.get('cube_position_z', 0)) cube_scale = st.slider("Cube Scale", min_value=0.1, max_value=5.0, value=st.session_state.get('cube_scale', 1.0)) show_bounding_box = st.checkbox("Show Bounding Box", value=st.session_state.get('show_bounding_box', True)) spotlight_intensity = st.slider("Spotlight Intensity", min_value=0.0, max_value=2.0, value=st.session_state.get('spotlight_intensity', 1.0)) ambient_light_intensity = st.slider("Ambient Light Intensity", min_value=0.0, max_value=2.0, value=st.session_state.get('ambient_light_intensity', 0.5)) camera_view = st.selectbox("Camera View", ["Default", "Top", "Side"], index=["Default", "Top", "Side"].index(st.session_state.get('camera_view', "Default"))) # Save settings button if st.button("Save Settings"): st.session_state['settings'] = { "cube_color": cube_color, "rotation_speed": rotation_speed, "cube_position_x": cube_position_x, "cube_position_y": cube_position_y, "cube_position_z": cube_position_z, "cube_scale": cube_scale, "show_bounding_box": show_bounding_box, "spotlight_intensity": spotlight_intensity, "ambient_light_intensity": ambient_light_intensity, "camera_view": camera_view } st.write("Settings saved:", st.session_state['settings']) # Load settings button if st.button("Load Settings"): st.write("Loading settings...") load_settings() st.write("Settings loaded:", st.session_state['settings']) st.rerun() def three_js_component(cube_color, rotation_speed, cube_position_x, cube_position_y, cube_position_z, cube_scale, show_bounding_box, spotlight_intensity, ambient_light_intensity, camera_view): component_code = f"""
""" components.html(component_code, height=600) with col2: st.title("3D Streamlit Component with Enhanced UI Controls") three_js_component(cube_color, rotation_speed, cube_position_x, cube_position_y, cube_position_z, cube_scale, show_bounding_box, spotlight_intensity, ambient_light_intensity, camera_view)