flexmeasures.ui.utils.view_utils

Utilities for views

Functions

flexmeasures.ui.utils.view_utils.accountname(account_id) str
flexmeasures.ui.utils.view_utils.asset_icon_name(asset_type_name: str) str

Icon name for this asset type.

This can be used for UI html templates made with Jinja. ui.__init__ makes this function available as the filter “asset_icon”.

For example:

<i class={{ asset_type.name | asset_icon }}></i>

becomes (for a battery):

<i class=”icon-battery”></i>

flexmeasures.ui.utils.view_utils.available_units() list[str]

Return a list of all available units from sensors currently in the database.

flexmeasures.ui.utils.view_utils.clear_session(keys_to_clear: list[str] = None)

Clear out session variables.

If keys_to_clear is provided, only clear out those specific session variables. Otherwise, clear out all session variables except for some special ones (e.g. Flask-Security’s, CSRF token, and our own session variables).

flexmeasures.ui.utils.view_utils.fall_back_to_flask_template(render_function)

In case the render_function is raising an error, fall back to using flask.render_template.

flexmeasures.ui.utils.view_utils.get_git_description() tuple[str, int, str]

Get information about the SCM (git) state if possible (if a .git directory exists).

Returns the latest git version (tag) as a string, the number of commits since then as an int and the current commit hash as string.

flexmeasures.ui.utils.view_utils.normalize_asset_type_name(asset_type_name: str) str

Reduce an asset type name to its lower-case alphanumeric characters, so that spelling variants share one mapping key.

For example, “charge-point”, “charge point” and “Charge_Point” all reduce to “chargepoint”.

flexmeasures.ui.utils.view_utils.render_flexmeasures_template(html_filename: str, **variables)

Render template and add all expected template variables, plus the ones given as **variables.

flexmeasures.ui.utils.view_utils.set_session_variables(*var_names: str, aliases: dict[str, str] | None = None)

Store request values as session variables, for a consistent UX across UI page loads.

Session values are always taken as the raw (string) request value, never a deserialized/typed one – downstream template rendering expects strings here (see the event_starts_after/event_ends_before handling in render_flexmeasures_template).

Parameters:

aliases – maps a var_name to an alternative request key to also check (e.g. its canonical spelling), for when a client may send either.

>>> set_session_variables("event_starts_after", "event_ends_before", "chart_type")
>>> set_session_variables(
...     "event_starts_after",
...     "event_ends_before",
...     aliases={"event_starts_after": "start", "event_ends_before": "end"},
... )
flexmeasures.ui.utils.view_utils.svg_asset_icon_name(asset_type_name: str) str

SVG icon URL for this asset type, as used in the asset structure view.

Asset type names are not restricted, so projects spell the same type in different ways. Matching therefore ignores case and separators, letting a type named “charge-point” share the icon mapped to “chargepoint”. A namespaced name, such as a plugin’s “myplugin.battery”, is matched on its last dot-separated component. An asset type we have no icon for falls back to a question mark.

flexmeasures.ui.utils.view_utils.username(user_id) str