Skip to content

Using Lets-plot chart in Shiny #448

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

Open
Boadzie opened this issue Apr 11, 2023 · 5 comments
Open

Using Lets-plot chart in Shiny #448

Boadzie opened this issue Apr 11, 2023 · 5 comments

Comments

@Boadzie
Copy link

Boadzie commented Apr 11, 2023

Is possible to use lets-plot in shiny? Lets-plot is an interactive version of the grammar of graphic in Python.

@nsiicm0

This comment was marked as outdated.

@Boadzie

This comment was marked as outdated.

@m-mahgoub

This comment was marked as duplicate.

@cpsievert
Copy link
Collaborator

cpsievert commented Jul 30, 2024

Here's a more minimal version using Shiny Express, and the more official .to_html() method:

import lets_plot as lp
import pandas as pd

from shiny.express import input, render, ui

datasets = {
    "iris": {
        "name": "Iris",
        "url": "https://raw.githubusercontent.com/JetBrains/lets-plot-docs/master/data/iris.csv",
    },
    "mpg": {
        "name": "Mpg",
        "url": "https://raw.githubusercontent.com/JetBrains/lets-plot-docs/master/data/mpg.csv",
    },
}

with ui.sidebar():
    ui.input_selectize(
        "dataset", "Dataset", {k: v.get("name") for k, v in datasets.items()}
    )


@render.ui
def letsplot():
    selection = input.dataset()
    df = pd.read_csv(datasets[selection]["url"])
    if selection == "iris":
        p = (
            lp.ggplot(df)
            + lp.geom_point(
                lp.aes("petal_length", "petal_width", color="species"), size=5
            )
            + lp.ggsize(1000, 500)
        )
    elif selection == "mpg":
        p = (
            lp.ggplot(df, lp.aes("displ", "cty", fill="drv", size="hwy"))
            + lp.geom_point(shape=21, color="white")
            + lp.scale_size(range=[5, 15], breaks=[15, 40])
            + lp.ggsize(1000, 500)
        )
    else:
        raise ValueError(f"{selection=} is not valid.")

    return ui.HTML(p.to_html(iframe=True))

@Boadzie
Copy link
Author

Boadzie commented Sep 21, 2024

This works great, thanks!

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

No branches or pull requests

4 participants