首页 > python教程

如何以Winsows Service方式运行JupyterLab

时间:2020-10-10 python教程 查看: 730

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

展开全文
上一篇:python exit出错原因整理
下一篇:Python生成并下载文件后端代码实例
输入字:
相关知识
Python 实现图片色彩转换案例

我们在看动漫、影视作品中,当人物在回忆过程中,体现出来的画面一般都是黑白或者褐色的。本文将提供将图片色彩转为黑白或者褐色风格的案例详解,感兴趣的小伙伴可以了解一下。

python初学定义函数

这篇文章主要为大家介绍了python的定义函数,具有一定的参考价值,感兴趣的小伙伴们可以参考一下,希望能够给你带来帮助,希望能够给你带来帮助

图文详解Python如何导入自己编写的py文件

有时候自己写了一个py文件,想要把它导入到另一个py文件里面,所以下面这篇文章主要给大家介绍了关于Python如何导入自己编写的py文件的相关资料,需要的朋友可以参考下

python二分法查找实例代码

二分算法是一种效率比较高的查找算法,其输入的是一个有序的元素列表,如果查找元素包含在列表中,二分查找返回其位置,否则返回NONE,下面这篇文章主要给大家介绍了关于python二分法查找的相关资料,需要的朋友可以参考下