Download src/utils/session_state.py from Khushal-ag/tabular-generation-test: direct link, hf CLI and curl.
- Browser
- Download file 3 kB
-
https://huggingface.co/spaces/Khushal-ag/tabular-generation-test/resolve/main/src/utils/session_state.py
- Command line
-
hf download hf://spaces/Khushal-ag/tabular-generation-test/src/utils/session_state.py
-
curl -L -o session_state.py https://huggingface.co/spaces/Khushal-ag/tabular-generation-test/resolve/main/src/utils/session_state.py
3 kB
| """ | |
| Session state management for the application | |
| """ | |
| import streamlit as st | |
| def initialize_session_state(): | |
| """Initialize all session state variables""" | |
| # API validation state | |
| if 'api_validation' not in st.session_state: | |
| st.session_state.api_validation = {"valid": False, "message": ""} | |
| # Track if validation was attempted (to control display of validation messages) | |
| if 'validation_attempted' not in st.session_state: | |
| st.session_state.validation_attempted = False | |
| # Validated API key (only set after server validation) | |
| if 'validated_api_key' not in st.session_state: | |
| st.session_state.validated_api_key = "" | |
| # Output format preference | |
| if 'output_format' not in st.session_state: | |
| st.session_state.output_format = "tabular" | |
| # Generated data storage | |
| if 'generated_data' not in st.session_state: | |
| st.session_state.generated_data = None | |
| # Last API key for validation tracking | |
| if 'last_api_key' not in st.session_state: | |
| st.session_state.last_api_key = "" | |
| # File processing state | |
| if 'file_processed' not in st.session_state: | |
| st.session_state.file_processed = False | |
| # Current file info | |
| if 'current_file_info' not in st.session_state: | |
| st.session_state.current_file_info = None | |
| # Configuration storage | |
| if 'config' not in st.session_state: | |
| st.session_state.config = None | |
| # UI state tracking to prevent duplication | |
| if 'ui_state_initialized' not in st.session_state: | |
| st.session_state.ui_state_initialized = True | |
| def reset_generation_state(): | |
| """Reset generation-related session state""" | |
| st.session_state.generated_data = None | |
| def reset_file_processing_state(): | |
| """Reset file processing-related session state""" | |
| st.session_state.file_processed = False | |
| st.session_state.current_file_info = None | |
| def get_session_value(key: str, default=None): | |
| """Get value from session state with default""" | |
| return st.session_state.get(key, default) | |
| def set_session_value(key: str, value): | |
| """Set value in session state""" | |
| st.session_state[key] = value | |
| def clear_file_state(): | |
| """Clear file-related session state when API key changes""" | |
| # Clear cloud file upload links | |
| keys_to_remove = [key for key in st.session_state.keys() if isinstance(key, str) and key.startswith('cloud_file_link_')] | |
| for key in keys_to_remove: | |
| del st.session_state[key] | |
| # Reset file processing state | |
| st.session_state.file_processed = False | |
| st.session_state.current_file_info = None | |
| st.session_state.generated_data = None | |
| def clear_api_state(): | |
| """Clear API-related session state""" | |
| st.session_state.api_validation = {"valid": False, "message": ""} | |
| st.session_state.validated_api_key = "" | |
| st.session_state.validation_attempted = False | |
| st.session_state.last_api_key = "" | |
| # Also clear dependent file state | |
| clear_file_state() | |