posit-dev / py-shinywidgets

Render ipywidgets inside a PyShiny app
MIT License
41 stars 2 forks source link

ipyvizzu support #141

Closed arky closed 3 months ago

arky commented 3 months ago

Description

Quick test to show iPyVizzu chart with py-shinywidget.

Don't know how to coerce <ipyvizzu.chart.Chart object at 0x7f0322007a30> into a ipywidget.Widget object. Instead of using shinywidgets to render this object, try using shiny's @render.ui decorator https://shiny.posit.co/py/api/ui.output_ui.html#shiny.ui.output_ui

What I Did


from shiny import reactive
from shiny.express import ui, render, input
from shinywidgets import render_widget  

import pandas as pd
from ipyvizzu import Chart, Data, Config

ui.panel_title("iPyVizzu Test")

@render_widget
def chart():
    df = pd.read_csv(
    "https://ipyvizzu.vizzuhq.com/latest/showcases/titanic/titanic.csv"
    )
    data = Data()
    data.add_df(df)

    chart = Chart(width="640px", height="360px")

    chart.animate(data)

    chart.animate(
        Config(
            {
                "x": "Count",
                "y": "Sex",
                "label": "Count",
                "title": "Passengers of the Titanic",
            }
        )
    )
    chart.animate(
        Config(
            {
                "x": ["Count", "Survived"],
                "label": ["Count", "Survived"],
                "color": "Survived",
            }
        )
    )
    chart.animate(Config({"x": "Count", "y": ["Sex", "Survived"]}))

    return chart
cpsievert commented 3 months ago

As that error message tries to point out, shinywidgets is only designed to work with ipywidgets, and ipyvizzu doesn't integrate with ipywidgets.

ipyvizzu does, however, implement a _repr_html_() method, and as a result, can be rendered via shiny's @render.ui decorator.

Unfortunately, to get the example working, you also need to set display=DisplayTarget.MANUAL in Chart():

import pandas as pd
from ipyvizzu import Chart, Config, Data, DisplayTarget
from shiny.express import input, render, ui

ui.panel_title("iPyVizzu Test")

@render.ui
def chart():
    df = pd.read_csv("https://ipyvizzu.vizzuhq.com/latest/showcases/titanic/titanic.csv")
    data = Data()
    data.add_df(df)

    chart = Chart(width="640px", height="360px", display=DisplayTarget.MANUAL)

    chart.animate(data)

    chart.animate(
        Config(
            {
                "x": "Count",
                "y": "Sex",
                "label": "Count",
                "title": "Passengers of the Titanic",
            }
        )
    )
    chart.animate(
        Config(
            {
                "x": ["Count", "Survived"],
                "label": ["Count", "Survived"],
                "color": "Survived",
            }
        )
    )
    chart.animate(Config({"x": "Count", "y": ["Sex", "Survived"]}))

    return chart
cpsievert commented 3 months ago

Closing this since, if there is anything to improve upon here, shinywidgets isn't the right place for it.

arky commented 3 months ago

Thank you @cpsievert That is really helpful