Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Better compatibility with existing notebooks #539

Open
maartenbreddels opened this issue Mar 6, 2024 · 0 comments
Open

Better compatibility with existing notebooks #539

maartenbreddels opened this issue Mar 6, 2024 · 0 comments

Comments

@maartenbreddels
Copy link
Contributor

maartenbreddels commented Mar 6, 2024

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
maartenbreddels added a commit that referenced this issue Mar 11, 2024
feat: improve matplotlib support - as Jupyter


Several improvements:

## Default backend
We do this by checking if the env var 'MPLBACKEND' is not set, and if so, we set it to
module://matplotlib_inline.backend_inline

## Support for display(figure)

This makes it behave more like the Jupyter notebook environment.

## Support matplotlib pylab interface in solara server

This makes code that 'just works' in the notebook also work in solara
server. We patch global dicts with scoped dicts.

See #539
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant