Skip to content

Better compatibility with existing notebooks #539

@maartenbreddels

Description

@maartenbreddels

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions