Skip to content

Utils

utils

Functions:

inputs_in_columns

inputs_in_columns(
    variable_names,
    component,
    key_prefix,
    default_value: float | Mapping[str, float],
    nb_columns=5,
)

Generate a list of input components according to a list of names.

The input components are placed in an array.

Parameters:

  • variable_names –

    A component is created for each name of this list.

  • component –

    The Streamlit component, either a text_input or a number_input.

  • key_prefix –

    The prefix applied to the key associated to the component, to ensure the key unique.

  • default_value (float | Mapping[str, float]) –

    The default value of the component. If passed as a float, the value is applied to all components. If passed as a dictionary, the value is applied to each component according to its name.

  • nb_columns –

    The number of columns of the array of components.

Source code in src\vimseo\dashboards\visualisation\utils.py
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
def inputs_in_columns(
    variable_names,
    component,
    key_prefix,
    default_value: float | Mapping[str, float],
    nb_columns=5,
):
    """Generate a list of input components according to a list of names.

    The input components are placed in an array.

    Args:
        variable_names: A component is created for each name of this list.
        component: The Streamlit component, either a text_input or a number_input.
        key_prefix: The prefix applied to the key associated to the component,
            to ensure the key unique.
        default_value: The default value of the component. If passed  as a ``float``,
            the value is applied to all components. If passed as a dictionary, the
            value is applied to each component according to its name.
        nb_columns: The number of columns of the array of components.
    """
    input_values = {}
    cols = st.columns(nb_columns)
    nb_full_rows = int(len(variable_names) / nb_columns)
    for i_row in range(nb_full_rows):
        for j, name in enumerate(
            variable_names[nb_columns * i_row : nb_columns + nb_columns * i_row]
        ):
            with cols[j]:
                input_values[name] = component(
                    name,
                    value=(
                        default_value[name]
                        if isinstance(default_value, dict)
                        else default_value
                    ),
                    key=f"{key_prefix}_{name}_{i_row}",
                )
    for j, name in enumerate(
        variable_names[nb_columns * nb_full_rows : len(variable_names)]
    ):
        with cols[j]:
            input_values[name] = component(
                name,
                value=(
                    default_value[name]
                    if isinstance(default_value, dict)
                    else default_value
                ),
                key=f"{key_prefix}_{name}",
            )
    return input_values

multiselect_with_all

multiselect_with_all(
    key,
    default_values,
    in_sidebar=False,
    help_: str | None = None,
)

A Streamlit container adding an All button to a multiselection.

Source code in src\vimseo\dashboards\visualisation\utils.py
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
def multiselect_with_all(
    key, default_values, in_sidebar=False, help_: str | None = None
):
    """A Streamlit container adding an ``All`` button to a multiselection."""
    with st.container():
        if in_sidebar:
            all_ = st.sidebar.checkbox("Select all variables", key=f"{key}_all")
            if all_:
                selected_values = st.sidebar.multiselect(
                    "Choose variable(s)",
                    default_values,
                    key=key,
                    default=default_values,
                    help=help_,
                )
            else:
                selected_values = st.sidebar.multiselect(
                    "Choose variable(s)",
                    default_values,
                    key=key,
                    help=help_,
                )
        else:
            all_ = st.checkbox("Select all variables", key=f"{key}_all")
            if all_:
                selected_values = st.multiselect(
                    "Choose variable(s)",
                    default_values,
                    key=key,
                    default=default_values,
                    help=help_,
                )
            else:
                selected_values = st.multiselect(
                    "Choose variable(s)",
                    default_values,
                    key=key,
                    help=help_,
                )
    return selected_values

st_create_model

st_create_model(model_name, lc_name, **options)

A Streamlit cached data function to create a model.

Source code in src\vimseo\dashboards\visualisation\utils.py
83
84
85
86
@st.cache_data
def st_create_model(model_name, lc_name, **options):
    """A Streamlit cached data function to create a model."""
    return create_model(model_name, lc_name, **options)