I have an altair chart and would like to make it interactive in the following way. When I click on a data point, I want an application to be started via its CLI, with attributes from the data point provided as arguments to the startup command. My understanding is that this should be possible using the new JupyterChart class introduced in Altair 5.3.0 – see an example here. However, following the example, when I try to run my application by making a call to subprocess.Popen
in my observer callback, nothing happens. What am I doing wrong?
Here’s a MWE:
import altair as alt
import pandas as pd
import subprocess
def on_click(change):
sel = change.new.value[0]
x_sel = df.iloc[sel]["x"]
p = subprocess.Popen(["echo", f"'{x_sel}'"])
df = pd.DataFrame({"x": [1,2], "y": [1,1]})
brush = alt.selection_point("brush")
jchart = alt.JupyterChart(
alt.Chart(df).mark_point(filled=True, size=100, stroke="black").encode(
x=alt.X("x:Q").scale(domain=[0,3]),
y=alt.Y("y:Q").scale(domain=[0,2]),
color=alt.Color("x:Q").legend(None)
)
)
jchart.selections.observe(on_click, ["brush"])
jchart
Here’s my environment info:
- python 3.11.9
- altair 5.3.0
- ipykernel 6.25.0 (required by VS Code)
- jupyter 1.0.0
- notebook 6.5.6
I tried running the MWE in both VS Code (1.91.1) and jupyter notebook. In VS Code, my editor window froze up: I could not switch focus to the terminal, and I could not right click on anything without immediately losing the context menu. In Jupyter Notebook, my window did not freeze, and I did not see any error messages in the interface or console, but I also did not see any output. In both cases, I expected to see the result of my Popen call (ie, “1” or “2”) printed in the cell output.