Skip to main content

Flexible interface for defining functions with marimo UI inputs

Project description

marimo-functions

Flexible interface for defining functions with marimo UI inputs

What is a "Marimo Function"

When running analysis in marimo, it is common to have a set of inputs which are all mapped as inputs to a particular function. The marimo-functions library provides a simple framework for implementing this pattern of user interaction.

The MarimoFunction class has an inputs attribute which maps keyword arguments to marimo UI elements. The type attribute of each input specifies the UI element, and all of the remaining kwargs are passed to the constructor.

The run() method then takes the kwargs provided by the user and runs any method that consumes them appropriately.

For example:

from marimo_functions import MarimoFunction
from sklearn.manifold import TSNE as SKTSNE


class TSNE(MarimoFunction):
    """
    A class representing a t-SNE (t-distributed Stochastic Neighbor Embedding) function.
    """
    inputs = {
        "n_components": {
            "type": "number",
            "label": "Number of components",
            "value": 2,
            "start": 1,
            "step": 1
        },
        "perplexity": {
            "type": "number",
            "label": "Perplexity",
            "value": 30,
            "start": 5,
            "stop": 50,
            "step": 1
        },
        "early_exaggeration": {
            "type": "number",
            "label": "Early exaggeration",
            "value": 12.0,
            "start": 1.0,
            "stop": 100.0,
            "step": 0.1
        },
        "random_state": {
            "type": "number",
            "label": "Random State",
            "value": 0
        }
    }
    name = "t-SNE"
    description = "T-distributed Stochastic Neighbor Embedding."

    @classmethod
    def run(cls, **kwargs) -> SKTSNE:
        return SKTSNE(**kwargs)

Using a Function

To make use of a function in marimo, simply prompt the user for the inputs:

# func: MarimoFunction
inputs = func.prompt()
inputs

Note that by using inputs as the last line in the marimo cell, it is presented as a UI element to the user.

And then run the function using the values provided by the user:

output = func.run(**inputs.value)

Picking a Function

In addition to the single function, this library provides a mechanism for selecting a function of interest from a dropdown menu.

from marimo_functions import PickFunction

selected_function = (
    PickFunction(
        functions=[
            FunctionA,
            FunctionB,
            FunctionC,
            FunctionD,
            FunctionE
        ]
    )
    .prompt(
        label="Select a function",
        value="Function A"
    )
)
selected_function

The function selected by the user can be populated by the value attribute.

func = selected_function.value

# Display the description of the function
mo.md(func.description)

Interactive Example

To run a marimo notebook displaying this functionality:

  1. Set up the local environment (uv sync)
  2. Launch the notebook (uv run marimo edit example.py)

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

marimo_functions-0.1.0.tar.gz (6.6 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

marimo_functions-0.1.0-py3-none-any.whl (8.8 kB view details)

Uploaded Python 3

File details

Details for the file marimo_functions-0.1.0.tar.gz.

File metadata

  • Download URL: marimo_functions-0.1.0.tar.gz
  • Upload date:
  • Size: 6.6 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.13.7

File hashes

Hashes for marimo_functions-0.1.0.tar.gz
Algorithm Hash digest
SHA256 0affcb3a5f33e8894e4f3121e19c781f49059572360fa3610eb59eb36f1192f1
MD5 a9dcbdd7234c0c3d2fc31e5f28d5bd4b
BLAKE2b-256 b2b62a21ef36bae9547765add22f545ca51822a3aec9188a40d25621505dabf7

See more details on using hashes here.

Provenance

The following attestation bundles were made for marimo_functions-0.1.0.tar.gz:

Publisher: release.yaml on FredHutch/marimo-functions

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file marimo_functions-0.1.0-py3-none-any.whl.

File metadata

File hashes

Hashes for marimo_functions-0.1.0-py3-none-any.whl
Algorithm Hash digest
SHA256 96329380af1b9ffd33b35a1f069bca12679513f09b918aaff883b5a534dc6b2d
MD5 eada4b2abf40025ae495fa609fb7a165
BLAKE2b-256 f73187f62a25c1d7646d876d4c7fedd4b1ab53ee994f5df8a8b023aa458855b7

See more details on using hashes here.

Provenance

The following attestation bundles were made for marimo_functions-0.1.0-py3-none-any.whl:

Publisher: release.yaml on FredHutch/marimo-functions

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page