Skip to content

Mapping dispatchers

Thor Whalen edited this page Aug 3, 2022 · 1 revision

In the following, we record some ideas of tools to dispatch mappings. Mappings are ubiquitous. Many data needs can be mapped a (possibly nested key-value data structure, including local/remote files, mongo documents, settings/configurations etc.

Some (or many) of these ideas might be target specific, but including here since it will serve to see estimate the line of generality through the points of the various concrete cases.

kv2info

Keywords: store dispatch, dol

The util function could look something like this:

from typing import Mapping, Callable, Dict, Any

Key = str  # or should we say Union[int, str], or more general?
Val = Any
Displayable = Any  # but want it to be displayable in the target context

Field = str
DisplayableFieldVal = Displayable  # the displayable information of a single field
PreviewValInfo = Dict[Field, DisplayableFieldVal]  # think; a row of a information table

SelectedValInfo = Displayable  # the information about the value for a key, which should be used to display more information if item is selected

def key_func_mapping_dispatcher(
    mapping: Mapping, 
    kv2info: Callable[[Key, Val], PreviewValInfo]
    extra_kv2info: Callable[[Key, Val], SelectedValInfo] = None
):
    """
    Dispatch mapping using one or two key functions to decide what to display for selection aid and for selected item preview

    :param mapping: The mapping to dispatch
    :param kv2info: The function that gives us some "displayable" information about a particular (key, value) pair.
        Meant to be used as a first level display of information about an item, for each item of the mapping
        (or a subset thereof if paging is implemented)
    :param extra_kv2info: A second function that gives some "displayable" information about a particular (key, value) pair.
        This optional function is meant to be used as a secondary, usually more detailed, information source to be displayed
        only on demand (say if the item is "selected")
    :return: A dispatchable object (e.g. a view)
    """

Here's a concrete example to help ground the above. Say we have a mapping whose keys are filepaths of .wav audio files. To be able to choose the audio file we want we'd like to see the file name along with the duration of the audio, with an option to hear a random 2 seconds of the audio if we want to peek further. The kv2info will return dicts such as {'filename': 'my_audio.wav', 'duration': 157} (which will require to have a view both on key and value (i.e. bytes, here) of the mapping) and the extra_kv2info would simply play a 2 second sample, somehow.

Note that the two kv functions need to have key and value arguments, but don't have to use both (or theoretically, either). If the information can be "computed" from the key (resp. value) alone, the function will only consider the key (resp. value).

The intended use is that this function would be partialized into a function that just takes a mapping. This "instance" of key_func_mapping_dispatcher would then be called on the mapping to implement the mapping dispatch.

Note that this pattern could be applied with CLI or GUI dispatches, but the body of key_func_mapping_dispatcher would differ in every concrete case. (Perhaps a class implementing patterns common to all cases would be appropriate here.).

streamlit's case.

In the case of streamlitfront, we'd like to harness streamlit's existing pandas.DataFrame capabilities.

I believe st.write recognizes dataframes automatically, but there's also a specific function to display dataframes called st.dataframe.

The streamlitfront could look something like this:

from itertools import starmap 

def st_key_func_mapping_dispatcher(
    mapping: Mapping, 
    kv2info: Callable[[Key, Val], PreviewValInfo],
    extra_kv2info: Callable[[Key, Val], SelectedValInfo] = None
):
    """key_func_mapping_dispatcher for streamlit"""
    mapping_info_df = pd.DataFrame(list(starmap(key2info, mapping.items())))
    
    if extra_kv2info is None:
        return st.dataframe(mapping_info_df)
    else:
        raise NotImplementedError("To be implemented")

Here's one thing I found on implementing a callback on row-selection. This is where we'd use extra_kv2info.

Clone this wiki locally