有數據分析,數據挖掘,以及機器學習和深度學習實踐經驗的讀者應該會對Jupyter Notebook這一工具十分熟悉,而JupyterLab是它的升級版本,其提供了更具擴展性,更加可定制化的功能選項。
安裝與啟動JupyterLab的方法與Jupyter Notebook一樣簡單。
應用安裝
pip install jupyterlab
應用啟動
jupyter lab
但這樣的操作會帶來一個問題,在以瀏覽器打開JupterLab應用窗口的同時,必須始終保證命令行窗口同樣處于打開狀態。如下圖所示:

要想解決這樣的問題,需要將JupyterLab以Windows Service的方式運行。
而Python代碼要在Windows系統里創建Service的話要用到win32serviceutil這個類庫。
類庫安裝
pip install pywin32
服務代碼
將以下代碼保存為jupyterlabservice.py文件,并放在配置目錄之下,如C:\Users\Ken\.jupyter。
import inspect
import logging
import os
import win32serviceutil
from jupyterlab.labapp import JupyterApp, LabApp
current_file = os.path.abspath(inspect.getfile(inspect.currentframe()))
os.chdir(os.path.dirname(current_file))
class JupyterLabService(win32serviceutil.ServiceFramework):
_svc_name_ = "JupyterLab"
_svc_display_name_ = "Jupyter Lab Service"
_svc_description_ = "Jupyter Lab Service"
def __init__(self, args):
super().__init__(args)
self.app = LabApp()
def _init_lab(self):
JupyterApp.initialize(self.app)
self.app.init_configurables()
self.app.init_components()
self.app.init_webapp()
self.app.init_terminals()
self.app.init_server_extensions()
self.app.init_mime_overrides()
self.app.init_shutdown_no_activity()
def SvcDoRun(self):
self.app.config_dir = "."
self._init_lab()
self.app.start()
def SvcStop(self):
self.app.stop()
def SvcShutdown(self):
self.SvcStop()
if __name__ == '__main__':
win32serviceutil.HandleCommandLine(JupyterLabService)
服務安裝
python .\jupyterlabservice.py install
服務啟動
python .\jupyterlabservice.py start
訪問localhost:8888網址,可以在瀏覽器中打開JupyterLab應用,但此時會遇到需要token認證的問題,如下圖所示:

解決此問題方法是修改配置文件中的token參數。
首先是在配置目錄中找到jupyter_notebook_config.py文件,如果沒有的話可以通過以下命令創建。
jupyter lab --generate-config
然后找到c.NotebookApp.token一項,將其值設為空字符串。
## Token used for authenticating first-time connections to the server.
#
# The token can be read from the file referenced by JUPYTER_TOKEN_FILE or set
# directly with the JUPYTER_TOKEN environment variable.
#
# When no password is enabled, the default is to generate a new, random token.
#
# Setting to an empty string disables authentication altogether, which is NOT
# RECOMMENDED.
c.NotebookApp.token = ''
重啟相應服務后,再次訪問localhost:8888網址,這下就正常了。

如果不想使用默認的8888端口,也可以在c.NotebookApp.port選項中將其值改成特定的端口號。
## The port the notebook server will listen on (env: JUPYTER_PORT).
c.NotebookApp.port = 9999
再次重啟服務,這次便可以通過localhost:9999來訪問JuypterLab應用了。
作者:Ken.W
出處:http://www.cnblogs.com/kenwoo
以上就是如何以Winsows Service方式運行JupyterLab的詳細內容,更多關于運行JupyterLab的資料請關注億速云其它相關文章!
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。