{"id":1606,"date":"2019-06-13T22:50:28","date_gmt":"2019-06-13T14:50:28","guid":{"rendered":"https:\/\/www.techcoil.com\/blog\/?p=1606"},"modified":"2020-05-12T10:55:56","modified_gmt":"2020-05-12T02:55:56","slug":"how-to-use-nssm-to-setup-jupyter-notebook-as-a-windows-service-running-its-own-python-3-virtual-environment","status":"publish","type":"post","link":"https:\/\/www.techcoil.com\/blog\/how-to-use-nssm-to-setup-jupyter-notebook-as-a-windows-service-running-its-own-python-3-virtual-environment\/","title":{"rendered":"How to use NSSM to setup Jupyter Notebook as a Windows Service running its own Python 3 virtual environment"},"content":{"rendered":"<p>When you take <a href=\"https:\/\/click.linksynergy.com\/deeplink?id=qGQokmUY1xA&mid=40328&murl=https%3A%2F%2Fwww.coursera.org%2Fspecializations%2Fdeep-learning\" rel=\"noopener\" target=\"_blank\">Andrew Ng's Deep Learning Coursera course<\/a>, you can get a better understanding of deep learning. In addition, you will get many <a href=\"https:\/\/jupyter.org\/\" rel=\"noopener\" target=\"_blank\">Jupyter<\/a> Notebook documents that you can reference while building models that can solve interesting problems.<\/p>\n<p>When you want to readily access those documents, you can setup Jupyter Notebook as a Windows Service on your <a href=\"https:\/\/www.amazon.com\/s\/ref=as_li_ss_tl?k=Windows+computer&ref=nb_sb_noss_2&linkCode=ll2&tag=clivsperswebs-20&linkId=63c73af27783b06d1b1109e442615954&language=en_US\" rel=\"noopener\" target=\"_blank\">Windows machine<\/a>.<\/p>\n<p>As I had mentioned in <a href=\"https:\/\/www.techcoil.com\/blog\/how-to-use-nssm-to-run-a-python-3-application-as-a-windows-service-in-its-own-python-3-virtual-environment\/\" rel=\"noopener\" target=\"_blank\">how to use NSSM to run a Python 3 application as a Windows Service in its own Python 3 virtual environment<\/a>, <a href=\"https:\/\/nssm.cc\/\" rel=\"noopener\" target=\"_blank\">NSSM<\/a> is a very helpful tool for us to create Windows Services.<\/p>\n<p>Since we can install Jupyter Notebook via <a href=\"https:\/\/pypi.org\/project\/pip\/\" rel=\"noopener\" target=\"_blank\">pip<\/a>, we can run Jupyter Notebook from its own virtual environment. In addition to that, since we can <a href=\"https:\/\/www.techcoil.com\/blog\/how-to-create-a-python-3-virtual-environment-in-windows-10\/\" rel=\"noopener\" target=\"_blank\">create a Python 3 virtual environment in Windows 10<\/a>, we can setup Jupyter Notebook as a Windows Service running its own Python 3 virtual environment.<\/p>\n<p>Given these points, let us look at how we can use NSSM to setup Jupyter Notebook as a Windows Service running its own Python 3 virtual environment.<\/p>\n<h2>Run a command prompt instance as administrator<\/h2>\n<p>In order to avoid being prompted for administrator access while using NSSM, let's run a command prompt instance as administrator.<\/p>\n<p><img decoding=\"async\" src=\"https:\/\/www.techcoil.com\/blog\/wp-content\/uploads\/Finding-Command-Prompt-and-running-it-as-administrator-on-Windows-10.gif\" alt=\"Finding Command Prompt and running it as administrator on Windows 10\"  \/><\/p>\n<p>We will run the subsequent commands in this command prompt instance.<\/p>\n<h2>Preparing the virtual environment to run your Jupyter Notebook instance<\/h2>\n<p>Before continuing on, be sure to go through the tutorial on <a href=\"https:\/\/www.techcoil.com\/blog\/how-to-create-a-python-3-virtual-environment-in-windows-10\/\" rel=\"noopener\" target=\"_blank\">how to create a Python 3 virtual environment in Windows 10<\/a>. After you had gone through the tutorial, you would have installed Python 3 on your Windows machine. In addition to that, you will understand how to create, activate and install dependencies into a virtual environment.<\/p>\n<h3>Creating the virtual environment to run our Python 3 application<\/h3>\n<p>Given these points, let us first create the virtual environment to run your Jupyter Notebook instance. In order to do so, start a command prompt window and type in the following command:<\/p>\n<pre class=\"brush: bash; title: ; notranslate\" title=\"\">\r\npython -m venv &quot;%systemdrive%%homepath%\\Documents\\jupyter-notebook-env&quot;\r\n<\/pre>\n<p>After the command completes, you will find the Python 3 virtual environment within the <code>%systemdrive%%homepath%\\Documents\\jupyter-notebook-env<\/code> folder.<\/p>\n<h2>Installing Jupyter Notebook into the Python 3 virtual environment on Windows<\/h2>\n<p>After you had created the Python 3 virtual environment to run your Jupyter Notebook instance, run the following command to activate it:<\/p>\n<pre class=\"brush: bash; title: ; notranslate\" title=\"\">\r\n&quot;%systemdrive%%homepath%\\Documents\\jupyter-notebook-env\\Scripts\\activate.bat&quot;\r\n<\/pre>\n<p>After you had activated your Python 3 virtual environment, you can install Jupyter Notebook with the following command:<\/p>\n<pre class=\"brush: bash; title: ; notranslate\" title=\"\">\r\npip install jupyter\r\n<\/pre>\n<p>When the command completes, you will be able to run an instance of Jupyter Notebook from your Python 3 virtual environment.<\/p>\n<h2>Creating a batch file to run Jupyter Notebook without starting a browser<\/h2>\n<p>At this point in time, we are ready to create a batch file to run Jupyter Notebook without starting a browser.<\/p>\n<p>In order to do so, let's create a <code>run_jupyter.bat<\/code> file inside the <code>%systemdrive%%homepath%\\Documents\\jupyter-notebook<\/code> folder with the following content:<\/p>\n<pre class=\"brush: plain; title: ; notranslate\" title=\"\">\r\ncall ..\\jupyter-notebook-env\\Scripts\\activate.bat\r\ncall jupyter notebook --ip 0.0.0.0 --port 12345 --no-browser\r\n<\/pre>\n<h2>Downloading a copy of NSSM<\/h2>\n<p>After you had created the windows batch file to run the an instance of Jupyter Notebook within the virtual environment, proceed to <a href=\"https:\/\/nssm.cc\/download\" rel=\"noopener\" target=\"_blank\">download NSSM<\/a>. Save the <code>.zip<\/code> file and extract its content. After extracting the content, you will find a <code>nssm.exe<\/code> file inside the win32 and win64 folder.<\/p>\n<p>Depending on <a href=\"https:\/\/www.techcoil.com\/blog\/how-to-determine-if-your-windows-10-iot-operating-system-is-32-bit-or-64-bit\/\" rel=\"noopener\" target=\"_blank\">whether your Windows is 32 bit or 64 bit<\/a>, you will use the corresponding nssm.exe file to install your Jupyter Notebook as a Windows Service.<\/p>\n<p>For the purpose of this tutorial, let's assume that we want to use the <code>nssm.exe<\/code> in the <code>C:\\nssm-2.24\\win64<\/code> folder.<\/p>\n<h2>Installing your Jupyter Notebook as a Windows Service with nssm.exe<\/h2>\n<p>Given that our nssm.exe is located in C:\\nssm-2.24\\win64, we can then use the following command to install our Python application as a Windows Service:<\/p>\n<pre class=\"brush: bash; title: ; notranslate\" title=\"\">\r\nC:\\nssm-2.24\\win64\\nssm install jupyter-notebook &quot;%systemdrive%%homepath%\\Documents\\jupyter-notebook\\run_jupyter.bat&quot;\r\n<\/pre>\n<p>When you run the command, NSSM will install your Jupyter Notebook as a Windows Service.<\/p>\n<p>At this point in time, <strong>your Windows Service is not yet started<\/strong>.<\/p>\n<h2>Enabling log files for your Jupyter Notebook<\/h2>\n<p>When you start your Windows Service at this point in time, it will not generate any log files when it runs. In such a situation, you will not be able to access Jupyter Notebook as we need to provide a token that Jupyter Notebook generates in standard error.<\/p>\n<p>Therefore, let's configure it to channel application output to log files.<\/p>\n<p>In order to do so, first start NSSM service editor with the following command:<\/p>\n<pre class=\"brush: bash; title: ; notranslate\" title=\"\">\r\nC:\\nssm-2.24\\win64\\nssm edit jupyter-notebook\r\n<\/pre>\n<p>After the command run, NSSM service editor will appear for enabling log files for your Jupyter Notebook.<\/p>\n<p><img decoding=\"async\" src=\"https:\/\/www.techcoil.com\/blog\/wp-content\/uploads\/nssm-2.24-service-editor-for-editing-Jupyter-Notebook-Windows-Service.gif\" alt=\"nssm-2.24 service editor for editing Jupyter Notebook Windows Service\"  \/><\/p>\n<p>Given that, click on the <strong>I\/O<\/strong> tab and enter the paths that you want the log files to appear:<\/p>\n<p><img decoding=\"async\" src=\"https:\/\/www.techcoil.com\/blog\/wp-content\/uploads\/nssm-2.24-service-editor-for-Jupyter-Notebook-Windows-Service-with-output-and-error-log-paths-set.gif\" alt=\"nssm-2.24 service editor for Jupyter Notebook Windows Service with output and error log paths set\" \/><\/p>\n<p>Once you are done with that, click <strong>Edit service<\/strong>.<\/p>\n<h2>Starting your Python application as a Windows Service<\/h2>\n<p>In order to start the Windows Service for your Jupyter Notebook, run the following command:<\/p>\n<pre class=\"brush: bash; title: ; notranslate\" title=\"\">\r\nC:\\nssm-2.24\\win64\\nssm start jupyter-notebook\r\n<\/pre>\n<p>Since <strong>Startup Type<\/strong> for the Windows Service is set to <strong>Automatic<\/strong>, our Jupyter Notebook will run automatically when we had started our Windows machine.<\/p>\n<h2>Accessing Jupyter Notebook for the first time<\/h2>\n<p>In order to access Jupyter Notebook for the first time, we need to check the token that is captured in the logs. Therefore, go to the stderr.log file that you had specified earlier. Once you open the log file, you should see output similar to the following:<\/p>\n<pre class=\"brush: plain; title: ; notranslate\" title=\"\">\r\n&#x5B;I 22:16:40.777 NotebookApp] Serving notebooks from local directory: C:\\Users\\ABC\\Documents\\jupyter-notebook\r\n&#x5B;I 22:16:40.777 NotebookApp] The Jupyter Notebook is running at:\r\n&#x5B;I 22:16:40.777 NotebookApp] http:\/\/(DESKTOP-ABCDEF or 127.0.0.1):12345\/?token=cca2b43a954dbccbcf264b6bbaba31339769e8a21e98f3b4\r\n&#x5B;I 22:16:40.777 NotebookApp] Use Control-C to stop this server and shut down all kernels (twice to skip confirmation).\r\n&#x5B;C 22:16:40.798 NotebookApp] \r\n    \r\n    To access the notebook, open this file in a browser:\r\n        file:\/\/\/C:\/WINDOWS\/system32\/config\/systemprofile\/AppData\/Roaming\/jupyter\/runtime\/nbserver-12932-open.html\r\n    Or copy and paste one of these URLs:\r\n        http:\/\/(DESKTOP-4SA4PFO or 127.0.0.1):12345\/?token=cca2b43a954dbccbcf264b6bbaba31339769e8a21e98f3b4\r\n\r\n<\/pre>\n<p>Given that, follow one of the instructions to access your Jupyter Notebook for the first time. In this case, I will open up a browser and enter <strong>http:\/\/localhost:12345\/?token=cca2b43a954dbccbcf264b6bbaba31339769e8a21e98f3b4<\/strong> into the location bar.<\/p>\n<p><img decoding=\"async\" src=\"https:\/\/www.techcoil.com\/blog\/wp-content\/uploads\/Home-page-of-Jupyter-Notebook-Windows-Service.gif\" alt=\"Home page of Jupyter Notebook Windows Service\" \/><\/p>\n<p>With that, you will be able to readily access your Jupyter Notebook documents whenever you need to.<\/p>\n\n      <ul id=\"social-sharing-buttons-list\">\n        <li class=\"facebook\">\n          <a href=\"https:\/\/www.facebook.com\/sharer\/sharer.php?u=https%3A%2F%2Fwp.me%2Fp245TQ-pU\" target=\"_blank\" role=\"button\" rel=\"nofollow\">\n            <img decoding=\"async\" src=\"\/ph\/img\/3rd-party\/social-icons\/Facebook.png\" alt=\"Facebook icon\"> Share\n          <\/a>\n        <\/li>\n        <li class=\"twitter\">\n          <a href=\"https:\/\/twitter.com\/intent\/tweet?text=&url=https%3A%2F%2Fwp.me%2Fp245TQ-pU&via=Techcoil_com\" target=\"_blank\" role=\"button\" rel=\"nofollow\">\n          <img decoding=\"async\" src=\"\/ph\/img\/3rd-party\/social-icons\/Twitter.png\" alt=\"Twitter icon\"> Tweet\n          <\/a>\n        <\/li>\n        <li class=\"linkedin\">\n          <a href=\"https:\/\/www.linkedin.com\/shareArticle?mini=1&title=&url=https%3A%2F%2Fwp.me%2Fp245TQ-pU&source=https:\/\/www.techcoil.com\" target=\"_blank\" role=\"button\" rel=\"nofollow\">\n          <img decoding=\"async\" src=\"\/ph\/img\/3rd-party\/social-icons\/linkedin.png\" alt=\"Linkedin icon\"> Share\n          <\/a>\n        <\/li>\n        <li class=\"pinterest\">\n          <a href=\"https:\/\/pinterest.com\/pin\/create\/button\/?url=https%3A%2F%2Fwww.techcoil.com%2Fblog%2Fwp-json%2Fwp%2Fv2%2Fposts%2F1606&description=\" class=\"pin-it-button\" target=\"_blank\" role=\"button\" rel=\"nofollow\" count-layout=\"horizontal\">\n          <img decoding=\"async\" src=\"\/ph\/img\/3rd-party\/social-icons\/Pinterest.png\" alt=\"Pinterest icon\"> Save\n          <\/a>\n        <\/li>\n      <\/ul>\n    ","protected":false},"excerpt":{"rendered":"<p>When you take <a href=\"https:\/\/click.linksynergy.com\/deeplink?id=qGQokmUY1xA&#038;mid=40328&#038;murl=https%3A%2F%2Fwww.coursera.org%2Fspecializations%2Fdeep-learning\" rel=\"noopener\" target=\"_blank\">Andrew Ng&#8217;s Deep Learning Coursera course<\/a>, you can get a better understanding of deep learning. In addition, you will get many <a href=\"https:\/\/jupyter.org\/\" rel=\"noopener\" target=\"_blank\">Jupyter<\/a> Notebook documents that you can reference while building models that can solve interesting problems.<\/p>\n<p>When you want to readily access those documents, you can setup Jupyter Notebook as a Windows Service on your <a href=\"https:\/\/www.amazon.com\/s\/ref=as_li_ss_tl?k=Windows+computer&#038;ref=nb_sb_noss_2&#038;linkCode=ll2&#038;tag=clivsperswebs-20&#038;linkId=63c73af27783b06d1b1109e442615954&#038;language=en_US\" rel=\"noopener\" target=\"_blank\">Windows machine<\/a>.<\/p>\n<p>As I had mentioned in <a href=\"https:\/\/www.techcoil.com\/blog\/how-to-use-nssm-to-run-a-python-3-application-as-a-windows-service-in-its-own-python-3-virtual-environment\/\" rel=\"noopener\" target=\"_blank\">how to use NSSM to run a Python 3 application as a Windows Service in its own Python 3 virtual environment<\/a>, <a href=\"https:\/\/nssm.cc\/\" rel=\"noopener\" target=\"_blank\">NSSM<\/a> is a very helpful tool for us to create Windows Services.<\/p>\n<p>Since we can install Jupyter Notebook via <a href=\"https:\/\/pypi.org\/project\/pip\/\" rel=\"noopener\" target=\"_blank\">pip<\/a>, we can run Jupyter Notebook from its own virtual environment. In addition to that, since we can <a href=\"https:\/\/www.techcoil.com\/blog\/how-to-create-a-python-3-virtual-environment-in-windows-10\/\" rel=\"noopener\" target=\"_blank\">create a Python 3 virtual environment in Windows 10<\/a>, we can setup Jupyter Notebook as a Windows Service running its own Python 3 virtual environment.<\/p>\n<p>Given these points, let us look at how we can use NSSM to setup Jupyter Notebook as a Windows Service running its own Python 3 virtual environment.<\/p>\n","protected":false},"author":1,"featured_media":1613,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"advanced_seo_description":"","jetpack_seo_html_title":"","jetpack_seo_noindex":false,"jetpack_post_was_ever_published":false,"_jetpack_newsletter_access":"","_jetpack_dont_email_post_to_subs":false,"_jetpack_newsletter_tier_id":0,"footnotes":""},"categories":[4],"tags":[528,652,226,233,195,438,181,347,21],"jetpack_featured_media_url":"https:\/\/www.techcoil.com\/blog\/wp-content\/uploads\/Home-page-of-Jupyter-Notebook-Windows-Service.gif","jetpack_shortlink":"https:\/\/wp.me\/p245TQ-pU","jetpack-related-posts":[],"jetpack_likes_enabled":true,"jetpack_sharing_enabled":true,"_links":{"self":[{"href":"https:\/\/www.techcoil.com\/blog\/wp-json\/wp\/v2\/posts\/1606"}],"collection":[{"href":"https:\/\/www.techcoil.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.techcoil.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.techcoil.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.techcoil.com\/blog\/wp-json\/wp\/v2\/comments?post=1606"}],"version-history":[{"count":0,"href":"https:\/\/www.techcoil.com\/blog\/wp-json\/wp\/v2\/posts\/1606\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.techcoil.com\/blog\/wp-json\/wp\/v2\/media\/1613"}],"wp:attachment":[{"href":"https:\/\/www.techcoil.com\/blog\/wp-json\/wp\/v2\/media?parent=1606"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.techcoil.com\/blog\/wp-json\/wp\/v2\/categories?post=1606"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.techcoil.com\/blog\/wp-json\/wp\/v2\/tags?post=1606"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}