widgetti / solara

A Pure Python, React-style Framework for Scaling Your Jupyter and Web Apps
https://solara.dev
MIT License
1.62k stars 105 forks source link

Better compatibility with existing notebooks #539

Open maartenbreddels opened 2 months ago

maartenbreddels commented 2 months ago

cc @koaning

While preparing a demo of https://github.com/koaning/drawdata using the original notebook to run on solara-server, we need to make the following changes.

Assign the widget to display to the page variable

This is mentioned in the tutorial for script: https://solara.dev/docs/tutorial/ipywidgets And also mentioned here: https://solara.dev/docs/reference/notebook-support

But, in notebook, we often display the last expression. I think we can do that if no Page or page variable is found.

We needed to modify the notebook like this:

-    "ipywidgets.HBox([widget, output])"
+    "page = ipywidgets.HBox([widget, output])\n",
+    "page"

Improved matplotlib support

Although we support matplotlib: https://solara.dev/api/matplotlib

Often, we find people using the pyplot interface, and expect plt.show to work. Although this uses a global object, and we live in a shared process (virtual kernels), this can be properly scoped.

We needed the following diff

-    "        plt.title(f\"{classifier.__class__.__name__}\");\n",
-    "        plt.show();\n",
-    "\n",
+    "        # plt.title(f\"{classifier.__class__.__name__}\");\n",
+    "        disp.ax_.set_title(f\"{classifier.__class__.__name__}\");\n",
+    "        # plt.show();\n",
+    "        import solara\n",
+    "        display(solara.FigureMatplotlib(fig))\n",

I think we can fix both issues, and run the notebook unmodified as:

$ solara run notebook.ipynb