Skip to content

streamlit_utils module

Contains various utility programs for Streamlit apps:

  • st_table_estimate: to print a table of estimates with standard errors
  • st_download_numpy_as_csv: button to download a Numpy array to a CSV file
  • st_download_dataframe_as_csv: button to download a Pandas dataframe to a CSV file.

st_download_dataframe_as_csv(df, file_name)

button to download a DataFrame to a csv file

Parameters:

Name Type Description Default
df DataFrame

a Pandas DataFrame

required
file_name str

the datafrme will be downloaded in file_name.csv

required

Returns:

Type Description
None

nothing

Source code in bs_python_utils/streamlit_utils.py
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
def st_download_dataframe_as_csv(df: pd.DataFrame, file_name: str) -> None:
    """button to download a DataFrame to a csv file

    Args:
        df: a Pandas DataFrame
        file_name: the datafrme will be downloaded in file_name.csv

    Returns:
        nothing
    """
    csv = _convert_dataframe_to_csv(df)
    _ = st.download_button(
        label=f"Download the {file_name} as a CSV file",
        data=csv,
        file_name=f"{file_name}.csv",
        mime="text/csv",
    )

st_download_numpy_as_csv(arr, file_name)

button to download an array to a csv file

Parameters:

Name Type Description Default
arr ndarray

a Numpy array

required
file_name str

the array will be downloaded in file_name.csv

required

Returns:

Type Description
None

nothing

Source code in bs_python_utils/streamlit_utils.py
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
def st_download_numpy_as_csv(arr: np.ndarray, file_name: str) -> None:
    """button to download an array to a csv file

    Args:
        arr: a Numpy array
        file_name: the array will be downloaded in file_name.csv

    Returns:
        nothing
    """
    csv = _convert_arr_to_csv(arr)
    _ = st.download_button(
        label=f"Download the {file_name} as a CSV file",
        data=csv,
        file_name=f"{file_name}.csv",
        mime="text/csv",
    )

st_table_estimates(coeff_names, estimates, stderrs, true_coeffs=None)

returns a table of the estimates

Parameters:

Name Type Description Default
coeff_names list[str]

the names of the coefficients

required
estimates ndarray

the estimated values of the coefficients

required
stderrs ndarray

the standard errors of the estimates

required
true_coeffs ndarray | None

the true values of the coefficients, if available

None

Returns:

Type Description
DataFrame

the table

Source code in bs_python_utils/streamlit_utils.py
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
def st_table_estimates(
    coeff_names: list[str],
    estimates: np.ndarray,
    stderrs: np.ndarray,
    true_coeffs: np.ndarray | None = None,
) -> pd.DataFrame:
    """returns a table of the estimates

    Args:
        coeff_names: the names of the coefficients
        estimates: the estimated values of the coefficients
        stderrs: the standard errors of the estimates
        true_coeffs: the true values of the coefficients, if available

    Returns:
        the table
    """
    st.write("The coefficients are:")
    if true_coeffs:
        df_coeffs_estimates = pd.DataFrame(
            {
                "True": true_coeffs,
                "Estimated": estimates,
                "Standard errors": stderrs,
            },
            index=coeff_names,
        )
    else:
        df_coeffs_estimates = pd.DataFrame(
            {
                "Estimated": estimates,
                "Standard errors": stderrs,
            },
            index=coeff_names,
        )

    return df_coeffs_estimates