`ninejs` is a Python package that adds browser interactivity to plotnine charts. It reads `tooltip`, `hover_group`, `hover_key`, and `on_click` from plotnine `aes()` mappings, then exports a self-contained HTML chart with inlined D3 and DOMPurify. - Docs: https://y-sunflower.github.io/ninejs/ - Repo: https://github.com/y-sunflower/ninejs - Install: `pip install ninejs` ## Imports ```python from ninejs import interactive, css, javascript, save, to_html, to_iframe, show ``` ## Composition Start with `interactive(gg)`, optionally add `css(...)` and/or `javascript(...)`, then end with one terminal action: ```python interactive(gg) + save("plot.html") interactive(gg) + to_html() interactive(gg) + to_iframe() interactive(gg) + show() ``` `css(...)` and `javascript(...)` can be chained multiple times. `save(...)` and `show()` return `None`; `to_html(...)` and `to_iframe(...)` return strings. In JupyterLab and VS Code notebooks, `interactive(gg)` renders automatically when it is the last expression in a cell. ## Public API `interactive(gg, *, hover_nearest=False, reverse_hover=False, zoomable=False, **savefig_kws)` - `gg`: a plotnine `ggplot`. - `hover_nearest`: when `True`, hover anywhere inside a panel to show the nearest interactive element. - `reverse_hover`: when `True`, dim the hovered group instead of dimming the non-hovered groups. - `zoomable`: whether to allow zoom on the chart. - `**savefig_kws`: forwarded to Matplotlib `Figure.savefig(..., format="svg")`, e.g. `dpi=200`. `css(from_string=None, *, from_dict=None, from_file=None)` - Provide exactly one source. - Raw string: `css(".tooltip { font-size: 18px; }")` - Dict: `css(from_dict={".tooltip": {"color": "red"}})`. - File: `css(from_file="style.css")` `javascript(from_string=None, *, from_file=None)` - Provide exactly one source. - Raw string: `javascript("console.log('ready')")` - File: `javascript(from_file="script.js")` - Runs as trusted JavaScript in the output page. `save(file_path, *, minify=True, extra_line=True)` - Writes a standalone HTML file. - `file_path`: output path. - `minify`: when `True`, removes whitespace between HTML tags. - `extra_line`: whether to add an extra blank line when minifying `to_html(*, minify=False, extra_line=True)` - Returns the standalone HTML as a string. - `minify`: when `True`, removes whitespace between HTML tags. - `extra_line`: whether to add an extra blank line when minifying `to_iframe(*, width="100%", height=600, title="ninejs interactive plot", sandbox="allow-scripts")` - Returns an ` ``` marimo: ```python import marimo as mo from ninejs import interactive, to_html mo.iframe(interactive(gg) + to_html()) ``` Shiny for Python: ```python from shiny import render, ui from ninejs import interactive, to_iframe @render.ui def chart(): return ui.HTML(interactive(plot()) + to_iframe(height="90%", width="70%")) ``` Streamlit: ```python import streamlit as st from ninejs import interactive, to_iframe st.iframe(interactive(gg) + to_iframe()) ``` Positron: ```python from ninejs import interactive, show interactive(gg) + show() ``` ## Notes - Generated HTML is self-contained and browser-only. - Tooltip HTML is sanitized with DOMPurify; custom `javascript(...)` and `on_click` code are trusted code and run directly in the browser. - `hover_nearest=True` builds a browser-side spatial index and samples path-like SVG elements, so very large or complex charts can take longer to initialize. - Only rely on the public imports listed here.