Streamlit Application for RegressionLab – web-based interface for curve fitting operations.
The streamlit_app.app module is the entry point for the web interface. The main UI logic lives in streamlit_app.sections; appearance is driven by streamlit_app.theme, which uses the same config as the Tkinter app (config.env, config.theme.UI_STYLE when available).
Package layout:
app.py– Entry point: page config, theme injection, session state, sidebar, mode routing.theme.py– Theme from config:get_streamlit_theme(),get_main_css(). Usesconfig.theme.UI_STYLE(env + theme) when importable; fallback toconfig.envonly. Sidebar background is slightly lighter than main area. Colors and fonts come fromUI_BACKGROUND,UI_FOREGROUND,UI_BUTTON_*,UI_FONT_*, etc.sections/sidebar.py– Sidebar setup, logo (or fallback header with theme colors), language toggle, session state. Initial language fromconfig.env(LANGUAGE).sections/data.py–load_uploaded_file,show_data_with_pair_plots,get_variable_names,get_temp_output_dir. Whenkey_prefixis set (e.g. View Data mode),show_data_with_pair_plotsalso renders transform, clean, download controls, and a Help expander with detailed option descriptions.sections/fitting.py–perform_fit,show_equation_selector,select_variables,show_plot_title_checkbox,create_equation_options. Usesconfig.FILE_CONFIGfor plot format/paths. Includes per-fit checkbox to toggle plot title visibility (default fromPLOT_SHOW_TITLE).sections/results.py–show_resultssections/help_section.py–show_help_section. Usesconfig.DONATIONS_URLfor the donations link.sections/modes.py–mode_normal_fitting,mode_multiple_datasets,mode_checker_fitting,mode_total_fitting,mode_view_data. Usesconfig.DATA_FILE_TYPES.
Imports: from streamlit_app import get_streamlit_theme, get_main_css; from streamlit_app.sections import mode_normal_fitting, perform_fit, load_uploaded_file, etc. (modes and helpers are in sections). The application offers the same functionality as the Tkinter desktop version, with the same configuration sources (env, paths, theme).
Main Streamlit application entry point.
This function sets up the Streamlit page configuration, injects theme CSS from config (get_streamlit_theme(), get_main_css()), caches the theme in st.session_state.streamlit_theme for reuse by sections (e.g. logo), initializes session state, displays the UI, and routes to the appropriate operation mode handler.
Example:
# Run with: streamlit run src/streamlit_app/app.py
if __name__ == "__main__":
main()The application supports five operation modes, aligned with the desktop version:
Single file, single equation fitting.
Function: mode_normal_fitting(equation_types: List[str]) -> None
Features:
- Optional loop fitting: checkbox to fit another file with the same equation without changing mode
- File upload (CSV, XLSX, TXT)
- Variable selection
- Equation selection (including custom)
- Checkbox to show/hide plot title (default from
PLOT_SHOW_TITLEenv var) - Single fit execution
- Result display (equation, parameters, statistics in three columns; plot; download below)
Example Usage:
from streamlit_app.sections import mode_normal_fitting
from config import AVAILABLE_EQUATION_TYPES
mode_normal_fitting(AVAILABLE_EQUATION_TYPES)Multiple files, single equation fitting.
Function: mode_multiple_datasets(equation_types: List[str]) -> None
Features:
- Multiple file upload
- Per-file variable selection
- Single equation for all files
- Checkbox to show/hide plot title (default from
PLOT_SHOW_TITLEenv var) - Batch fitting
- Results for each dataset
Example Usage:
from streamlit_app.sections import mode_multiple_datasets
mode_multiple_datasets(AVAILABLE_EQUATION_TYPES)Single file, multiple equations fitting.
Function: mode_checker_fitting(equation_types: List[str]) -> None
Features:
- Single file upload
- Variable selection
- Multiple equation selection
- Checkbox to show/hide plot title (default from
PLOT_SHOW_TITLEenv var) - Compare fits
- Results comparison
Example Usage:
from streamlit_app.sections import mode_checker_fitting
mode_checker_fitting(AVAILABLE_EQUATION_TYPES)Single file, all equations fitting.
Function: mode_total_fitting(equation_types: List[str]) -> None
Features:
- Single file upload
- Variable selection
- Checkbox to show/hide plot title (default from
PLOT_SHOW_TITLEenv var) - Automatic fitting with all equations
- Comprehensive comparison
- All results displayed
Example Usage:
from streamlit_app.sections import mode_total_fitting
mode_total_fitting(AVAILABLE_EQUATION_TYPES)View data from a file without fitting. Includes transform, clean, and download options.
Function: mode_view_data(equation_types: List[str]) -> None
Features:
- File upload (CSV, XLSX, TXT)
- Data table and optional pair plots
- Transform: Dropdown (FFT, DCT, Hilbert, Laplace, cepstrum, Hadamard, envelope, log, exp, sqrt, standardize, normalize, etc., plus inverses) and Apply button. Updates table in place.
- Clean: Dropdown (drop NaN, drop duplicates, fill NaN, remove outliers) and Apply button.
- Download: Format selector (CSV, TXT, XLSX) and download button for the current (possibly transformed/cleaned) data.
- No equation selection or fitting
Example Usage:
from streamlit_app.sections import mode_view_data
mode_view_data(AVAILABLE_EQUATION_TYPES)Load data from uploaded file.
Parameters:
uploaded_file: Streamlit UploadedFile object
Returns:
- DataFrame with loaded data or
Noneif loading fails
Example:
uploaded_file = st.file_uploader("Upload file", type=['csv', 'xlsx'])
if uploaded_file:
data = load_uploaded_file(uploaded_file)
if data is not None:
st.dataframe(data)Show data in an expander with optional pair plots and data analysis (transform, clean, download).
Parameters:
data: DataFrame or data to displaykey_prefix: Optional. If set (e.g. 'view_data'), enables transform/clean/download and uses session state for the current data. Required for analysis features.file_id: Optional. Whenkey_prefixis set, used to detect file changes. Whenfile_idchanges, the displayed data is reset to the new loaded data.
When key_prefix is set:
- Renders transform dropdown and Apply button (FFT, DCT, Hilbert, Laplace, log, etc.).
- Renders clean dropdown and Apply button (drop NaN, outliers, etc.).
- Renders format selector and download button for the current data.
- Pair plots use the current (possibly transformed) data. When there are more than 10 variables, a multiselect lets the user choose which to display (max 10 for readability).
- Renders Help expander with pair plots, transform options (each detailed), clean options (each detailed), and save. Same content as Tkinter help dialog; localized (Spanish, English, German).
perform_fit(data, x_name, y_name, equation_name, plot_name, custom_formula=None, parameter_names=None) -> Optional[Dict[str, Any]]
Perform curve fitting and return results. Uses the backend fit function (from fitting.get_fitting_function or custom evaluator); the backend may return (text, y_fitted, equation, fit_info) — only the first three values are used.
Parameters:
data: DataFrame with datax_name: X variable namey_name: Y variable nameequation_name: Type of equation to fitplot_name: Name for the plotcustom_formula: Optional custom formulaparameter_names: Optional parameter names for custom formula
Returns:
- Dictionary with fitting results or
Noneif fitting fails
Result Dictionary:
{
'equation_name': str, # Display name
'parameters': str, # Formatted parameters and statistics (plain text)
'equation': str, # Formula and formatted equation
'plot_path': str, # Path to saved plot (PNG/JPG/PDF)
'plot_name': str, # Plot name
'plot_path_display': str # Optional: path to PNG preview when plot_path is PDF
}Example:
from streamlit_app.sections import perform_fit
result = perform_fit(
data=data,
x_name='x',
y_name='y',
equation_name='linear_function',
plot_name='my_fit'
)
if result:
st.image(result['plot_path'], width='stretch')
with open(result['plot_path'], 'rb') as f:
st.download_button("Download", data=f.read(), file_name=result['plot_name'] + '.png')Sidebar and shared UI are implemented in streamlit_app.sections.sidebar and other section modules.
Setup the application sidebar.
The sidebar contains:
- Brand header: Application name and version
- Language selector: Toggle between Spanish and English
- Operation mode selector: Radio for Normal Fitting, Multiple Datasets, Checker Fitting, Total Fitting, View Data
Parameters:
version: Application version string
Returns:
- Selected operation mode
Display expandable help section.
Shows information about:
- Application objective
- Advantages
- Fitting modes explanation
- Data format requirements
Show variable selection widgets and return selected values.
Parameters:
data: DataFrame with datakey_prefix: Optional prefix for Streamlit widget keys
Returns:
- Tuple of
(x_name, y_name, plot_name)
Show equation type selector and return selection.
Parameters:
equation_types: List of available equation type names
Returns:
- Tuple of
(equation_name, custom_formula, parameter_names)
Features:
- Dropdown for predefined equations
- Custom formula input with parameter configuration
- Formula examples
Display fitting results.
Parameters:
results: List of result dictionaries fromperform_fit()
Layout: Three columns (equation left, parameters center, statistics right), then the plot image, then the download button below the plot.
- Column 1 – Equation: Formula and formatted equation with fitted values
- Column 2 – Parameters: Fit parameters with uncertainties and IC 95%
- Column 3 – Statistics: R², RMSE, χ², χ²_red, degrees of freedom
- Plot: Full-width below the columns
- Download: Button below the plot (saves PNG/JPG/PDF; when output format is PDF, in-app preview uses PNG)
Initialize Streamlit session state variables.
Variables:
language: Current language (initialized fromconfig.envLANGUAGE; 'es', 'en', etc.)results: List of fitting resultsplot_counter: Counter for plot filenames
Cycle to the next supported language (es → en → de → es).
Updates the session state language and re-initializes the i18n system.
Configuration is shared with the Tkinter app: config.env (.env), config.paths, and when available config.theme. The Streamlit UI does not provide an in-app configuration dialog; edit .env (or use the Tkinter Configure menu) to change language, UI colors, fonts, plot style, paths, etc.
st.set_page_config(
page_title="RegressionLab",
page_icon="📊",
layout="wide",
initial_sidebar_state="collapsed"
)- Source:
streamlit_app/theme.py. Theme is built fromconfig.theme.UI_STYLEwhen importable (same env + theme as Tkinter); otherwise fromconfig.envonly. Usesconfig.color_utilsforlighten_hexandmuted_from_hex(sidebar background, muted text). All colors are converted to hex for CSS (no tkinter at runtime in theme). - Applied in:
app.pycallsget_streamlit_theme()(cached inst.session_state.streamlit_theme), thenget_main_css(theme), and injects the returned CSS once per run. - Rules: Main area uses
UI_BACKGROUNDandUI_FOREGROUND; sidebar uses a slightly lighter background (sidebar_bg); buttons useUI_BUTTON_BGandUI_BUTTON_FG; headings/accents use primary and accent2; fonts fromUI_FONT_FAMILYandUI_FONT_SIZE. - Sidebar layout:
sections/sidebar.pydefines layout-only CSS (brand, version badge, section labels); colors come from the global theme CSS.
# From project root
streamlit run src/streamlit_app/app.py
# Or using the provided script
bin/run_streamlit.sh # Linux/Mac
bin/run_streamlit.bat # Windowsimport streamlit as st
from streamlit_app.sections import perform_fit, load_uploaded_file
from config import DATA_FILE_TYPES
st.title("Custom Fitting Interface")
uploaded_file = st.file_uploader("Upload data", type=list(DATA_FILE_TYPES))
if uploaded_file:
data = load_uploaded_file(uploaded_file)
if data is not None:
x_name = st.selectbox("X variable", data.columns)
y_name = st.selectbox("Y variable", data.columns)
if st.button("Fit Linear"):
result = perform_fit(
data, x_name, y_name,
'linear_function', 'fit'
)
if result:
st.image(result['plot_path'], width='stretch')The application uses temporary directories for plot storage in Streamlit Cloud:
Function: get_temp_output_dir() -> Path
Creates a session-specific temporary directory that persists during the session.
- Supports CSV, XLSX, TXT formats
- Files are temporarily saved for processing
- Automatic cleanup after processing
- Language toggle in sidebar
- All UI text translated
- Language persists in session state
- Automatic re-initialization on toggle
try:
from config import __version__, AVAILABLE_EQUATION_TYPES
except ImportError as e:
st.error(f"Error importing configuration: {e}")try:
result = perform_fit(...)
except FittingError as e:
st.error(f"Fitting failed: {e}")All errors are logged and displayed to the user with helpful messages.
-
Session State: Use session state for persistent data
if 'data' not in st.session_state: st.session_state.data = None
-
File Upload: Always validate uploaded files
if uploaded_file is not None: if uploaded_file.size > MAX_SIZE: st.error("File too large")
-
Progress Indicators: Show progress for long operations
with st.spinner("Fitting..."): result = perform_fit(...)
-
Error Messages: Provide clear error messages
if result is None: st.error("Fitting failed. Please check your data.")
- Streamlit: Web framework
- Pandas: Data handling
- NumPy: Numerical operations
- Matplotlib: Plot generation
- All RegressionLab modules: For fitting functionality
- Lazy Loading: Heavy imports loaded only when needed
- Caching: Logo and static content cached
- Temporary Files: Efficient cleanup after use
The application is designed for deployment on:
- Streamlit Cloud: Automatic deployment
- Local Server: For development
- Docker: Containerized deployment
- Modern browsers (Chrome, Firefox, Safari, Edge)
- Responsive design
- Mobile-friendly interface
For more information about the Streamlit interface, see Streamlit Guide.