Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: allow Menu to select a sql expression #299

Closed
wants to merge 1 commit into from

Conversation

frtennis1
Copy link
Contributor

Closes #298

Example usage of this feature,

df = pd.DataFrame({"x_val": np.arange(20)}).assign(
    y_val=lambda df: df["x_val"] * df["x_val"],
    noise=np.random.normal(scale=5, size=20),
)

d = {
    "vconcat": [
        {
            "input": "menu",
            "label": 'yCol',
            "options": [
                {"label": "Pure signal", "value": {"sql": "y_val"}},
                {"label": "Small noise", "value": {"sql": "y_val+noise"}},
                {"label": "Medium noise", "value": {"sql": "y_val+2*noise"}},
                {"label": "Large noise", "value": {"sql": "y_val+5*noise"}},
            ],
            "as": "$yCol",
        },
        {
            "plot": [
                {"mark": "lineY", "x": "x_val", "y": {"sql": "$yCol"}, "data": {"from": "df"}}
            ],
        },
    ],
}


MosaicWidget(d, data={'df': df})
image image

@jheer
Copy link
Member

jheer commented May 29, 2024

We don't plan to support expressions as menu options at the moment. While I'm sure it's just meant to be illustrative, the specific example above can be achieved by having the input provide a numerical value (noise factor) and then using it that in a sql expression elsewhere via a param reference.

I can also imagine people wanting to have string-valued menu options that correspond to database column names. As of this comment, this is not supported in declarative specs using params because a param string value will be interpreted as a string literal when referenced in downstream SQL expressions. Feel free to open a new issue to request some mechanism to instead interpret strings as column names or expressions (possibly as a type hint as part of a param definition?).

@jheer jheer closed this May 29, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Allow for Menu to select a sql expression
2 participants