当前位置: 移动技术网 > 科技>操作系统>windows > 如何以Winsows Service方式运行JupyterLab

如何以Winsows Service方式运行JupyterLab

2020年08月31日  | 移动技术网科技  | 我要评论
有数据分析,数据挖掘,以及机器学习和深度学习实践经验的读者应该会对jupyter notebook这一工具十分熟悉,而jupyterlab是它的升级版本,其提供了更具扩展性,更加可定制化的功能选项。安

有数据分析,数据挖掘,以及机器学习和深度学习实践经验的读者应该会对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的资料请关注移动技术网其它相关文章!

如您对本文有疑问或者有任何想说的,请点击进行留言回复,万千网友为您解惑!

相关文章:

验证码:
移动技术网