目录

  • 一、测试cell
  • 二、插入图片
  • 三、更改jupyter的默认文件路径
  • 四、plt图像输出
  • 自动输出图像
  • 取消自动输出图像
  • 五、输出变量
  • 1、最后一行输出
  • 2、单个文件多行输出
  • 3、所有文件多行输出
  • 六、运行py文件
  • 七、预先import库
  • 八、默认导入库
  • 九、导出隐藏代码块的html文件
  • 十、显示cell的行数
  • 十一、预览md文件


一、测试cell

在需要测试的cell中先输入以下语句

%%timeit

二、插入图片

将cell更改为markdown模式,并将需要插入的图片上传至自己的jupyter文件夹,然后在cell中输入以下语句:

<img src="filename.jpg" width=40% align=right>

src:文件名

width:图片的相对大小

align:图片的位置

三、更改jupyter的默认文件路径

对于jupyterlab,打开cmd,输入命令:

jupyter lab --generate-config

然后在.jupyter文件夹中可以找到jupyter_lab_config.py文件
打开进行编辑,找到#c.ServerApp.notebook_dir='' 将注释号#删除,并在引号内填入想要添加的默认路径,便可以成功修改了

对于jupyter notebook,则打开cmd,输入命令:

jupyter notebook --generate-config

然后在.jupyter文件夹中可以找到jupyter_notebook_config.py文件
打开进行编辑,找到#c.NotebookApp.notebook_dir='' 将注释号#删除,并在引号内填入想要添加的默认路径,便可以成功修改了

四、plt图像输出

自动输出图像

在使用matplotlib.pyplot画图时,可以删去plt.show(),直接采用如以下例子所示:

# 附加地址的图像输出
plt.plot(params)

jupyterlab开发环境的镜像 jupyterlab怎么运行代码_代码块

# 不附加地址的图像输出
plt.plot(params);

jupyterlab开发环境的镜像 jupyterlab怎么运行代码_jupyterlab开发环境的镜像_02

取消自动输出图像

plt.plot等绘图函数后增加plt.close()函数即可

jupyterlab开发环境的镜像 jupyterlab怎么运行代码_python_03

五、输出变量

1、最后一行输出

在每个cell的最后一行,单独的变量不需要print()就可以直接输出:

jupyterlab开发环境的镜像 jupyterlab怎么运行代码_jupyterlab开发环境的镜像_04

2、单个文件多行输出

在第一个cell中加上:

from IPython.core.interactiveshell import InteractiveShell
InteractiveShell.ast_node_interactivity = "all"

在接下来的cell中所有单独的变量都会被直接输出

jupyterlab开发环境的镜像 jupyterlab怎么运行代码_html_05

3、所有文件多行输出

在命令行中输入如下语句,创建ipython_config.py文件

ipython profile create

jupyterlab开发环境的镜像 jupyterlab怎么运行代码_Shell_06


~\.ipython\profile_default路径中找到ipython_config.py文件并打开,找到c.InteractiveShell.ast_node_interactivity的值,去掉注释号,并修改成'all'

jupyterlab开发环境的镜像 jupyterlab怎么运行代码_html_07


保存后就可以在所有文件都直接输出单独变量了

六、运行py文件

在cell中输入以下语句

%run filename.py

七、预先import库

在第一个cell中先将常用的库import完并运行,则后面的cell需要使用这些库的时候可以直接使用

八、默认导入库

找到文件~/.ipython/profile_default/ipython_config.py 第一次需要先创建该文件
在文件中加入(这里以导入numpy和matplotlib为例):

c = get_config()
c.InteractiveShellApp.exec_lines = [
    'import numpy as np\n'
    'from matplotlib import pyplot as plt\n'
]

九、导出隐藏代码块的html文件

在jupyterlab中新建Terminal

jupyterlab开发环境的镜像 jupyterlab怎么运行代码_python_08


在Terminal中输入

jupyter nbconvert ipynb文件的绝对路径 --to=html --TemplateExporter.exclude_input=True 即可生成同名的html文件,且该文件的所有代码块均被隐藏

十、显示cell的行数

点击jupyterlab界面中的view

jupyterlab开发环境的镜像 jupyterlab怎么运行代码_jupyterlab开发环境的镜像_09


在弹出的选项中点击Show Line Numbers

jupyterlab开发环境的镜像 jupyterlab怎么运行代码_html_10

十一、预览md文件

右键点击jupyter中的.md文件,选择Open With中的Markdown Preview即可

jupyterlab开发环境的镜像 jupyterlab怎么运行代码_python_11