Skip to content

Jupyter notebooks ​

With the jupyter extra, charts are drawn in notebooks:

sh
pip install "geneviewer-py[jupyter]"

The charts are anywidget widgets, so they work in JupyterLab, Jupyter Notebook, VS Code, Google Colab and marimo.

A chart in a cell ​

A cell that ends with a chart draws it below the cell:

python
from geneviewer import GC, datasets

clusters = datasets.load("ophA_clusters")
ophA = clusters[clusters["cluster"] == "ophA"]

chart = GC(ophA, group="class", height="150px")

The chart is the live chart of the other pages of this site: hover a gene for its tooltip, and the chart for the button that saves it as SVG or PNG.

Events ​

To read what happens in a chart, keep it as a widget with .widget(). The events switched on with .events() set attributes of the widget:

python
widget = (
    GC(clusters, cluster="cluster", group="class", height="300px")
    .labels("name")
    .zoom()
    .events(select=True, drag=True, zoom=True)
    .widget()
)
widget

After a click on a gene, a later cell can read it:

python
widget.select["data"]["name"]
AttributeValueSwitched on by
widget.clickThe element clickedclick=True (the default)
widget.dblclickThe element double-clickeddblclick=True
widget.selectThe element selected lastselect=True
widget.selectionEvery selected element, as a listselect=True
widget.dragAn element moved by dragging, and its offsetdrag=True
widget.movesThe elements a reset, undo or redo moveddrag=True
widget.zoomThe window of the zoom sliderzoom=True
widget.renderedThe chart was drawnrendered=True

Each is None until its event happens, and then a dict (a list for selection); Events describes what each holds. These are the values a Shiny app gets as input.<id>_<event>().

widget.on() calls a function at each event, also when the same event happens twice:

python
selected = []
widget.on("select", selected.append)

Moving the zoom slider ​

widget.zoom_to() sets the window of a chart with a zoom slider (.zoom()), without drawing the chart again. None shows everything:

python
widget.zoom_to([0, 0.5])
widget.zoom_to(None)

With .events(zoom=True), widget.zoom["range"] is the window the reader picked, which can go back into zoom_to().

Drawing another chart ​

Setting widget.chart draws another chart in the same place, for example the same data with another theme:

python
widget.chart = widget.chart.theme("dark")

Toolbar icons ​

The toolbar's own icons need nothing. Custom icons, set with .toolbar(), are Font Awesome icons by name and need Font Awesome on the page. JupyterLab does not load it, so load it in a cell:

python
from IPython.display import HTML

HTML(
    '<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/'
    '@fortawesome/fontawesome-free@6/css/all.min.css">'
)