Jupyter Notebook和JupyterLab的安装与使用

Jupyter Notebook和JupyterLab

Jupyter 和 JupyterLab 都是用于交互式计算和文档编写的工具,主要用于数据分析、机器学习和研究等领域。

Jupyter Notebook

Jupyter Notebook 是 Jupyter 项目最早发布的工具,提供了一个基于浏览器的交互式计算环境,支持多种编程语言(如 Python、R、Julia 等)。

Jupyter Notebook 使用类似于网页的界面,用户可以在单元格中编写和执行代码,并在 Markdown 格式的单元格中编写文本说明、公式、图像等内容。

Jupyter Notebook 支持大量的扩展,用户可以通过安装插件对其进行定制和增强功能。

JupyterLab

JupyterLab 是 Jupyter 项目的新一代用户界面,旨在取代传统的 Jupyter Notebook。它提供了更加灵活和强大的交互式环境,集成了许多实用功能。

JupyterLab 在一个界面中整合了文本编辑器、终端、文件浏览器、绘图窗口等多种功能,用户可以更方便地组织和管理多个文档和活动。

JupyterLab 支持拖放式界面布局、多文档编辑、命令面板等现代化功能,同时也兼容 Jupyter Notebook 格式,并提供了插件系统进行定制和扩展。

Jupyter Notebook的使用

安装

使用Jupyter Notebook,首先需要安装Jupyter Notebook。可以在终端或者命令提示符中输入以下命令进行安装:

pip install jupyter

启动

输入以下命令启动Jupyter Notebook

(demo) C:\Users\Administrator\Desktop\demo>jupyter notebook

  _   _          _      _
 | | | |_ __  __| |__ _| |_ ___
 | |_| | '_ \/ _` / _` |  _/ -_)
  \___/| .__/\__,_\__,_|\__\___|
       |_|

Read the migration plan to Notebook 7 to learn about the new features and the actions to take if you are using extensions.

https://jupyter-notebook.readthedocs.io/en/latest/migrate_to_notebook7.html

Please note that updating to Notebook 7 might break some of your extensions.

[I 12:30:16.232 NotebookApp] Serving notebooks from local directory: C:\Users\Administrator\Desktop\demo
[I 12:30:16.232 NotebookApp] Jupyter Notebook 6.5.3 is running at:
[I 12:30:16.232 NotebookApp] http://localhost:8888/?token=be4f7170251bd601ebef2693d3c32612944eed07566fef8b
[I 12:30:16.232 NotebookApp]  or http://127.0.0.1:8888/?token=be4f7170251bd601ebef2693d3c32612944eed07566fef8b
[I 12:30:16.232 NotebookApp] Use Control-C to stop this server and shut down all kernels (twice to skip confirmation).
[C 12:30:16.337 NotebookApp]

    To access the notebook, open this file in a browser:
        file:///C:/Users/Administrator/AppData/Roaming/jupyter/runtime/nbserver-54004-open.html
    Or copy and paste one of these URLs:
        http://localhost:8888/?token=be4f7170251bd601ebef2693d3c32612944eed07566fef8b
     or http://127.0.0.1:8888/?token=be4f7170251bd601ebef2693d3c32612944eed07566fef8b
[I 12:30:23.906 NotebookApp] 302 GET / (::1) 0.000000ms

会自动弹出浏览器窗口打开Jupyter Notebook的界面,在这个界面中,可以创建新的Notebook文件或者打开已有的文件,也可以进行其他操作。
在这里插入图片描述
若想让notebook打开指定目录,只要进入指定目录后执行命令即可

本地notebook的默认URL为http://localhost:8888/

创建文件

新建notebook文档,点击 New -> Python 3,即可创建一个新的 Python 3文档,然后就可以在Notebook中编写代码并运行

notebook的文档格式是.ipynb

在这里插入图片描述

编写代码和文本

新建notebook文档后,自动跳转到内容操作界面
在这里插入图片描述

在Notebook中,每个单元格都可以包含代码和文本,可以随时切换编辑模式(输入代码)和命令模式(运行代码)。

在单元格可以编写代码和文本,支持Markdown语法,可以添加标题、列表、表格、公式等。

在单元格中输入代码后点击Run,结果会直接显示在单元格下方。
在这里插入图片描述

常用命令

jupyter notebook: 启动 Jupyter Notebook

Ctrl + C:终止 Jupyter Notebook 运行

Shift + Enter:运行当前单元格,并移到下一个单元格

Alt + Enter:运行当前单元格,并在下方插入一个新的单元格

Ctrl + Enter:运行当前单元格,但不移动到下一个单元格

A:在当前单元格上方插入一个新的单元格

B:在当前单元格下方插入一个新的单元格

D, D:删除当前单元格

Z:撤销删除操作

M:将当前单元格转换为 Markdown 单元格

Y:将当前单元格转换为代码单元格

Shift + Up/Down:选中多个单元格

Shift + M:合并选中的单元格

配置文件

Jupyter Notebook 配置文件可以用于设置 Jupyter Notebook 的默认选项,例如默认使用的浏览器、默认工作目录、默认启用的扩展等。

查看配置文件

>jupyter-notebook --generate-config
Writing default config to: C:\Users\Administrator\.jupyter\jupyter_notebook_config.py

查看配置文件的信息

# Configuration file for jupyter-notebook.

c = get_config()  #noqa

#------------------------------------------------------------------------------
# Application(SingletonConfigurable) configuration
#------------------------------------------------------------------------------
## This is an application.

## The date format used by logging formatters for %(asctime)s
#  Default: '%Y-%m-%d %H:%M:%S'
# c.Application.log_datefmt = '%Y-%m-%d %H:%M:%S'

## The Logging format template
#  Default: '[%(name)s]%(highlevel)s %(message)s'
# c.Application.log_format = '[%(name)s]%(highlevel)s %(message)s'

## Set the log level by value or name.
#  Choices: any of [0, 10, 20, 30, 40, 50, 'DEBUG', 'INFO', 'WARN', 'ERROR', 'CRITICAL']
#  Default: 30
# c.Application.log_level = 30

## Configure additional log handlers.

注意:

两个井号##开头:是注释文本

一个井号#开头:是默认的配置信息

设置jupyter的目录的

# c.NotebookNotary.data_dir = ''

自动打开浏览器

# c.NotebookApp.open_browser = True

默认浏览器更改为 Firefox

# c.NotebookApp.browser = 'firefox'

添加中文选项

Jupyter Notebook默认是英文的
在这里插入图片描述
下载中文包

pip install jupyterlab-language-pack-zh-CN

刷新网页后选择中文即可。
在这里插入图片描述

JupyterLab的使用

安装

安装JupyterLab

pip install jupyterlab

启动

启动JupyterLab

jupyter lab

配置

生成Jupyter Lab配置文件

以root用户为例,生成位置:Writing default config to: /root/.jupyter/jupyter_lab_config.py

jupyter lab --generate-config

打开上面执行输出的jupyter_lab_config.py配置文件后,修改以下配置项:

c.ServerApp.allow_root = True # 非root用户启动,无需修改
c.ServerApp.ip = '*'

启动

前台启动

root@master: # jupyter lab
[I 2024-03-27 06:21:18.376 ServerApp] jupyter_lsp | extension was successfully linked.
[I 2024-03-27 06:21:18.380 ServerApp] jupyter_server_terminals | extension was successfully linked.
[I 2024-03-27 06:21:18.386 ServerApp] jupyterlab | extension was successfully linked.
[I 2024-03-27 06:21:18.615 ServerApp] notebook_shim | extension was successfully linked.
[W 2024-03-27 06:21:18.631 ServerApp] WARNING: The Jupyter server is listening on all IP addresses and not using encryption. This is not recommended.
[I 2024-03-27 06:21:18.632 ServerApp] notebook_shim | extension was successfully loaded.
[I 2024-03-27 06:21:18.634 ServerApp] jupyter_lsp | extension was successfully loaded.
[I 2024-03-27 06:21:18.635 ServerApp] jupyter_server_terminals | extension was successfully loaded.
[I 2024-03-27 06:21:18.636 LabApp] JupyterLab extension loaded from /usr/local/lib/python3.10/dist-packages/jupyterlab
[I 2024-03-27 06:21:18.636 LabApp] JupyterLab application directory is /usr/local/share/jupyter/lab
[I 2024-03-27 06:21:18.637 LabApp] Extension Manager is 'pypi'.
[I 2024-03-27 06:21:18.691 ServerApp] jupyterlab | extension was successfully loaded.
[I 2024-03-27 06:21:18.692 ServerApp] Serving notebooks from local directory: /usr/local/program/jupyterlab
[I 2024-03-27 06:21:18.692 ServerApp] Jupyter Server 2.13.0 is running at:
[I 2024-03-27 06:21:18.692 ServerApp] http://localhost:8888/lab?token=a8ad08f1c29003c7e056cd5550d101eddaaf63e762b15bac
[I 2024-03-27 06:21:18.692 ServerApp]     http://127.0.0.1:8888/lab?token=a8ad08f1c29003c7e056cd5550d101eddaaf63e762b15bac
[I 2024-03-27 06:21:18.692 ServerApp] Use Control-C to stop this server and shut down all kernels (twice to skip confirmation).

使用nohup后台启动Jupyter Lab

nohup jupyter lab --port=9999 --NotebookApp.token='Admin@2024' --notebook-dir=./ &

访问:IP:9999并输入设置的密码
在这里插入图片描述
在这里插入图片描述

添加中文选项

Jupyterlab默认是英文的
在这里插入图片描述
下载中文包

pip install jupyterlab-language-pack-zh-CN

刷新网页后选择中文即可。
在这里插入图片描述

Docker安装Jupyter Notebook

创建配置目录

mkdir -p /usr/local/docker/jupyter
chmod 777 -R /usr/local/docker/jupyter

启动jupyter服务

docker run --name myjupyter -d -p 8080:8888 -v /usr/local/docker/jupyter:/home/jovyan jupyter/base-notebook

访问Jupyter

浏览器访问:http://IP:8080/login
在这里插入图片描述

查看Token

[root@master docker]# docker exec -it myjupyter jupyter notebook list
Currently running servers:
http://0.0.0.0:8888/?token=0e21d1dbf3cdd5fece775bf3b0b53476d386da6726f7d098 :: /home/jovyan

登录

使用获取的Token在浏览器中输入然后登录
在这里插入图片描述

修改密码

[root@master docker]# docker exec -it myjupyter jupyter notebook password
Enter password: 
Verify password: 
[NotebookPasswordApp] Wrote hashed password to /home/jovyan/.jupyter/jupyter_notebook_config.json
[root@master docker]# docker restart myjupyter 
jupyter

在这里插入图片描述

Docker安装JupyterLab

创建容器

docker run -d --name jupyter  --gpus all  -v ./jupyter:/home/jovyan -p 8888:8888 -e JUPYTER_ENABLE_LAB=yes jupyter/datascience-notebook

查看token

通过容器日志命令:docker logs juypter查看

root@master: # docker logs jupyter
[I 2024-03-27 06:39:12.026 LabApp] JupyterLab application directory is /opt/conda/share/jupyter/lab
[I 2024-03-27 06:39:12.026 LabApp] Extension Manager is 'pypi'.
[I 2024-03-27 06:39:12.029 ServerApp] jupyterlab | extension was successfully loaded.
[I 2024-03-27 06:39:12.034 ServerApp] jupyterlab_git | extension was successfully loaded.
[I 2024-03-27 06:39:12.037 ServerApp] nbclassic | extension was successfully loaded.
[I 2024-03-27 06:39:12.092 ServerApp] nbdime | extension was successfully loaded.
[I 2024-03-27 06:39:12.096 ServerApp] notebook | extension was successfully loaded.
[I 2024-03-27 06:39:12.096 ServerApp] Serving notebooks from local directory: /home/jovyan
[I 2024-03-27 06:39:12.096 ServerApp] Jupyter Server 2.8.0 is running at:
[I 2024-03-27 06:39:12.096 ServerApp] http://dfed81e56824:8888/lab?token=c3532d39a1fb8ec4b69dc1e6d6f89a5fe323abf11d784f10
[I 2024-03-27 06:39:12.096 ServerApp]     http://127.0.0.1:8888/lab?token=c3532d39a1fb8ec4b69dc1e6d6f89a5fe323abf11d784f10
[I 2024-03-27 06:39:12.096 ServerApp] Use Control-C to stop this server and shut down all kernels (twice to skip confirmation).
[C 2024-03-27 06:39:12.099 ServerApp] 
    
    To access the server, open this file in a browser:
        file:///home/jovyan/.local/share/jupyter/runtime/jpserver-7-open.html
    Or copy and paste one of these URLs:
        http://dfed81e56824:8888/lab?token=c3532d39a1fb8ec4b69dc1e6d6f89a5fe323abf11d784f10
        http://127.0.0.1:8888/lab?token=c3532d39a1fb8ec4b69dc1e6d6f89a5fe323abf11d784f10
[I 2024-03-27 06:39:13.114 ServerApp] Skipped non-installed server(s): bash-language-server, dockerfile-language-server-nodejs, javascript-typescript-langserver, jedi-language-server, julia-language-server, pyright, python-language-server, python-lsp-server, r-languageserver, sql-language-server, texlab, typescript-language-server, unified-language-server, vscode-css-languageserver-bin, vscode-html-languageserver-bin, vscode-json-languageserver-bin, yaml-language-server

访问

访问:IP:8888,在Password or token输入密码
在这里插入图片描述
在这里插入图片描述

Anaconda

Anaconda是一个Python发行版和软件包管理器,它提供了许多常用的数据科学工具和库,并且可以方便地安装、配置和管理这些工具和库。

官网:https://www.anaconda.com/download/

Anaconda还附带了Jupyter Notebook,Jupyter Notebook是一个交互式笔记本,可以在网页浏览器中使用Python和其他编程语言来探索数据、构建模型和分享代码等。

总之,Anaconda是一个非常强大和灵活的Python数据科学生态系统,可以根据需要使用其中的任何组件。

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

CodeDevMaster

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

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

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

打赏作者

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

抵扣说明:

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

余额充值