{"id":151,"date":"2024-06-19T20:29:02","date_gmt":"2024-06-19T18:29:02","guid":{"rendered":"https:\/\/www.nerdluecht.de\/?p=151"},"modified":"2024-06-19T20:29:02","modified_gmt":"2024-06-19T18:29:02","slug":"jupyter-notebooks-in-vs-code-with-interactive-matplotlib-figures-and-persistent-sessions","status":"publish","type":"post","link":"https:\/\/www.nerdluecht.de\/index.php\/2024\/06\/19\/jupyter-notebooks-in-vs-code-with-interactive-matplotlib-figures-and-persistent-sessions\/","title":{"rendered":"Jupyter Notebooks in VS Code with Interactive Matplotlib Figures and Persistent Sessions"},"content":{"rendered":"\n<p><strong>TL;DR:<\/strong> I run a Jupyter Notebook server in a <code>tmux<\/code> session and configure VS Code to connect to that. I use <code><a rel=\"noreferrer noopener\" href=\"https:\/\/github.com\/matplotlib\/ipympl\" data-type=\"URL\" data-id=\"https:\/\/github.com\/matplotlib\/ipympl\" target=\"_blank\">ipympl<\/a><\/code> to display interactive Matplotlib figures in VS Code.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Persistent Sessions<\/h2>\n\n\n\n<p>Generally, VS Code shuts down a Jupyter Kernel when is closed. This might be disadvantageous if it contains costly computations (like training of machine learning models). Then, it should continue to run in the background, even if the user is not connected.<\/p>\n\n\n\n<p>To run a Jupyter notebook server continuously, I use <code>tmux<\/code>.<\/p>\n\n\n\n<p>VS Code allows to specify a remote (or local) Jupyter Server to connect to when viewing notebooks: Press Ctlr+Shift+P to access the Command Palette and search for &#8220;Jupyter: Specify server for connections&#8221;:<\/p>\n\n\n\n<figure class=\"wp-block-image size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"678\" height=\"165\" src=\"https:\/\/www.nerdluecht.de\/wp-content\/uploads\/2022\/05\/image.png\" alt=\"\" class=\"wp-image-153\" srcset=\"https:\/\/www.nerdluecht.de\/wp-content\/uploads\/2022\/05\/image.png 678w, https:\/\/www.nerdluecht.de\/wp-content\/uploads\/2022\/05\/image-300x73.png 300w\" sizes=\"(max-width: 678px) 100vw, 678px\" \/><\/figure>\n\n\n\n<p>Start the notebook server and copy its URL into the configuration field<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>$ jupyter notebook --no-browser\n    ...\n&#91;I 22:07:43.456 NotebookApp] Jupyter Notebook 6.4.0 is running at:\n&#91;I 22:07:43.457 NotebookApp] http:\/\/localhost:8891\/?token=deadbeef\n&#91;I 22:07:43.457 NotebookApp]  or http:\/\/127.0.0.1:8891\/?token=deadbeef\n&#91;I 22:07:43.457 NotebookApp] Use Control-C to stop this server and shut down all kernels (twice to skip confirmation).\n&#91;C 22:07:43.483 NotebookApp] \n    \n    To access the notebook, open this file in a browser:\n        file:\/\/\/...\/nbserver-10655-open.html\n    Or copy and paste one of these URLs:\n        http:&#47;&#47;localhost:8891\/?token=deadbeef\n     or http:\/\/127.0.0.1:8891\/?token=deadbeef<\/code><\/pre>\n\n\n\n<figure class=\"wp-block-image size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"642\" height=\"87\" src=\"https:\/\/www.nerdluecht.de\/wp-content\/uploads\/2022\/05\/image-1.png\" alt=\"\" class=\"wp-image-154\" srcset=\"https:\/\/www.nerdluecht.de\/wp-content\/uploads\/2022\/05\/image-1.png 642w, https:\/\/www.nerdluecht.de\/wp-content\/uploads\/2022\/05\/image-1-300x41.png 300w\" sizes=\"(max-width: 642px) 100vw, 642px\" \/><\/figure>\n\n\n\n<p>Then, new notebooks will be opened using the configured notebook server.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Interactive Matplotlib Figures<\/h2>\n\n\n\n<p>By default, Matplotlib figures are static in VS Code. This can be changed by using the <code>ipympl<\/code> Matplotlib backend. It can be installed via Conda (<code>conda install -c conda-forge ipympl<\/code>) and has to be activated using the following cell magic:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>%matplotlib widget<\/code><\/pre>\n\n\n\n<h2 class=\"wp-block-heading\">References<\/h2>\n\n\n\n<ul>\n<li><a href=\"https:\/\/github.com\/matplotlib\/ipympl\" target=\"_blank\" rel=\"noreferrer noopener\">https:\/\/github.com\/matplotlib\/ipympl<\/a><\/li>\n\n\n\n<li><a href=\"https:\/\/stackoverflow.com\/a\/64614116\/1116842\" target=\"_blank\" rel=\"noreferrer noopener\">https:\/\/stackoverflow.com\/a\/64614116\/1116842<\/a><\/li>\n\n\n\n<li><a href=\"https:\/\/blog.ouseful.info\/2019\/02\/11\/connecting-to-a-remote-jupyter-notebook-server-running-on-digital-ocean-from-microsoft-vs-code\/\" target=\"_blank\" rel=\"noreferrer noopener\">https:\/\/blog.ouseful.info\/2019\/02\/11\/connecting-to-a-remote-jupyter-notebook-server-running-on-digital-ocean-from-microsoft-vs-code\/<\/a><\/li>\n<\/ul>\n\n\n\n<p><\/p>\n","protected":false},"excerpt":{"rendered":"<p>TL;DR: I run a Jupyter Notebook server in a tmux session and configure VS Code to connect to that. I use ipympl to display interactive Matplotlib figures in VS Code. Persistent Sessions Generally, VS Code shuts down a Jupyter Kernel when is closed. This might be disadvantageous if it contains costly computations (like training of &hellip; <\/p>\n<p class=\"link-more\"><a href=\"https:\/\/www.nerdluecht.de\/index.php\/2024\/06\/19\/jupyter-notebooks-in-vs-code-with-interactive-matplotlib-figures-and-persistent-sessions\/\" class=\"more-link\">Continue reading<span class=\"screen-reader-text\"> &#8220;Jupyter Notebooks in VS Code with Interactive Matplotlib Figures and Persistent Sessions&#8221;<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[14],"tags":[17,21,22,18,20],"_links":{"self":[{"href":"https:\/\/www.nerdluecht.de\/index.php\/wp-json\/wp\/v2\/posts\/151"}],"collection":[{"href":"https:\/\/www.nerdluecht.de\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.nerdluecht.de\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.nerdluecht.de\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.nerdluecht.de\/index.php\/wp-json\/wp\/v2\/comments?post=151"}],"version-history":[{"count":5,"href":"https:\/\/www.nerdluecht.de\/index.php\/wp-json\/wp\/v2\/posts\/151\/revisions"}],"predecessor-version":[{"id":171,"href":"https:\/\/www.nerdluecht.de\/index.php\/wp-json\/wp\/v2\/posts\/151\/revisions\/171"}],"wp:attachment":[{"href":"https:\/\/www.nerdluecht.de\/index.php\/wp-json\/wp\/v2\/media?parent=151"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.nerdluecht.de\/index.php\/wp-json\/wp\/v2\/categories?post=151"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.nerdluecht.de\/index.php\/wp-json\/wp\/v2\/tags?post=151"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}