Kanaries / pygwalker

PyGWalker: Turn your pandas dataframe into an interactive UI for visual analysis
https://kanaries.net/pygwalker
Apache License 2.0
11.08k stars 570 forks source link

feat: add new api to_chart_html #470

Closed longxiaofei closed 6 months ago

longxiaofei commented 6 months ago

new api to_chart_html

API Reference

def to_chart_html(
    dataset: Union[DataFrame, Connector, str],
    spec: Dict[str, Any],
    *,
    spec_type: Literal["graphic-walker", "vega"] = "graphic-walker",
    theme_key: Literal['vega', 'g2'] = 'g2',
    dark: Literal['media', 'light', 'dark'] = 'media',
) -> str:
    """
    Generate HTML code of a chart by graphic-walker or vega spec.

    Args:
        - dataset (pl.DataFrame | pd.DataFrame | Connector, optional): dataset.
        - spec (Dict[str, Any]): chart config data.

    Kargs:
        - spec_type (Literal["graphic-walker", "vega"]): type of spec.
        - theme_key ('vega' | 'g2'): theme type.
        - dark ('media' | 'light' | 'dark'): 'media': auto detect OS theme.
    """
    pass

Example

from pygwalker.api.html import to_chart_html
from IPython.display import display, HTML

html = to_chart_html(
    df,
    {
        "mark": "bar",
        "encoding": {
            "x": {"field": "season"},
            "y": {"field": "casual", "aggregate": "sum"},
            "color": {"field": "holiday"},
        }
    },
    spec_type="vega"
)

display(HTML(html))

image