远程访问服务器上的Jupyterlab

前言



Juputerlab被称为下一代的notebook,其功能强大并且拥有丰富的插件,现在已经有很多公司都在使用,比如旷视的MegStudio。

安装conda

可以不安装conda,也可以安装miniconda或者anaconda。
根据我的喜好,我这里推荐大家使用轻便的miniconda而不是庞大的Anaconda,安装方式

安装jupyterlab

安装完conda之后,在base下安装jupyterlab
可以使用pip或conda安装Jupyterlab

使用pip安装

pip是python自带的包管理工具,如果使用pip安装,请在命令行执行:

pip install jupyterlab

使用conda安装

如果用conda安装,请在命令行执行:

conda install -c conda-forge jupyterlab

启动jupyterlab

最简单的方式是

jupyter lab

启动服务之后会自动打开浏览器

不想自动打开浏览器可以加参数--no-browser

jupyter lab --no-browser

默认端口是8888,也可以指定端口号

jupyter lab --port 8889

配置jupyterlab使用conda环境

一般不同的项目会有不同的依赖,我们可以通过conda管理,避免包之间的冲突
假设我们已经创建好了名为py38的conda环境
创建conda环境的方式点击
激活环境并安装ipykernel,然后进行配置

conda activate py38  #先激活环境
pip install ipykernel  #安装 ipykernel 库
ipython kernel install --user --name=py38  #设置kernel,--user表示你的当前账户下的,py38 为kernel名

重新运行jupyter lab即可切换conda的python环境

具体怎么切换呢?
如果是已经有ipynb文件,那就直接打开,然后点击右上角的python3,在对话框Select Kernel中选择你要的环境,
或者是点击最底下的Python 3 | Idle进行切换;
如果是新建文件的话,点击右上角的New Launcher+号进行选择,或者是File > New Launcher

查看已经建立好的映射:

jupyter kernelspec list

删除映射:

jupyter kernelspec remove py38

设置显示名称--display-name

ipython kernel install --user --name=py38 --display-name "Python3.8 GPU"

远程访问服务jupyterlab

配置远程访问,可以通过ip和端口号随时随地访问服务,方便开发

无密码无token访问

ip+port直接访问(不安全):

# 服务端运行
jupyter lab --port 5000 --no-browser --ip='*' --ServerApp.token='' --ServerApp.password=''
# 本地浏览器
http://192.168.0.xxx:5000/

密码登录

# 生成hash密码
from jupyter_server.auth import passwd
passwd()
# 'sha1:51ef0f912be4:e77e57968f64be1818f836315465a4dc39fdd35c'
# 生成配置文件
jupyter lab --generate-config

编辑修改配置文件,并将上面生成的密码hash值整个复制到c.ServerApp.password

## /home/ubuntu/.jupyter/jupyter_lab_config.py
c.LabApp.open_browser = False
#------------------------------------------------------------------------------
# ServerApp(JupyterApp) configuration
#------------------------------------------------------------------------------

## Allow requests where the Host header doesn't point to a local server
#  
#  By default, requests get a 403 forbidden response if the 'Host' header shows
#  that the browser thinks it's on a non-local domain. Setting this option to
#  True disables this check.
#  
#  This protects against 'DNS rebinding' attacks, where a remote web server
#  serves you a page and then changes its DNS to send later requests to a local
#  IP, bypassing same-origin checks.
#  
#  Local IP addresses (such as 127.0.0.1 and ::1) are allowed as local, along
#  with hostnames configured in local_hostnames.
#  Default: False
c.ServerApp.allow_remote_access = True

## The IP address the Jupyter server will listen on.
#  Default: 'localhost'
c.ServerApp.ip = '*'

## Whether to open in a browser after starting. The specific browser used is
#  platform dependent and determined by the python standard library `webbrowser`
#  module, unless it is overridden using the --browser (ServerApp.browser)
#  configuration option.
#  Default: False
c.ServerApp.open_browser = False

## Hashed password to use for web authentication.
#  
#  To generate, type in a python/IPython shell:
#  
#    from jupyter_server.auth import passwd; passwd()
#  
#  The string should be of the form type:salt:hashed-password.
#  Default: ''
c.ServerApp.password = 'sha1:51ef0f912be4:e77e57968f64be1818f836315465a4dc39fdd35c'

## The port the Jupyter server will listen on.
#  Default: 8888
c.ServerApp.port = 8889

保存之后启动服务

jupyter lab
# 本地浏览器
http://192.168.0.xxx:8889/lab

小坑

刚开始时生成密码hash,使用的是

from IPython.lib import passwd
passwd()

生成的是字段设置之后,怎么也登录不成功


上面配置的是内网ip,所以只有局域网能访问到,如果要外网能访问到,需要配置成公网ip,做端口映射。
  • 3
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 6
    评论
评论 6
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

ayiya_Oese

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值