【Jupyter】个人开发常见命令
1. 查看python版本
import sys
print(sys.version)
2. ipynb/py文件转换
jupyter nbconvert --to python my_file.ipynbipynb转换为mdjupyter nbconvert --to md my_file.ipynbipynb转为htmljupyter nbconvert --to html my_file.ipynbipython转换为pdfjupyter nbconvert --to pdf my_file.ipynb
py --> ipynbIn [ ]:%run file.py加载了file.py文件,相当于导包。In [ ]:%load file.py把flie.py的代码显示出来
3. magic 函数
%! [cmd]
%time
%matplotlib inline
%history
%load file.py
4. plotly
jupyter notebook可以直接在里边画图,并以html显示出来。
import plotly.offline as py
py.init_notebook_mode(connected=True)
!但是jupyter lab不可以,需要安装其他的插件。
5. tqdm
解决tqdm不能显示的问题:
#usual installation
pip install ipywidgets
jupyter nbextension enable --py widgetsnbextension
#you are my saver!
jupyter labextension install @jupyter-widgets/jupyterlab-manager
6. 展示所有行所有列
import pandas as pd
pd.set_option('display.width', 500) #设置整体宽度
pd.set_option('display.height', 500) #设置整体高度
pd.set_option('display.max_rows',100) #设置最大行数
pd.set_option('display.max_columns', 100) #设置最大列数#显示所有列
pd.set_option('display.max_columns', None)
#显示所有行
pd.set_option('display.max_rows', None)# 以下可能存在问题
pd.set_option('max_columns',100)
pd.set_option('max_row',100)
7. 单cell多变量输出
from IPython.core.interactiveshell import InteractiveShell
InteractiveShell.ast_node_interactivity = "all"
8. 刷新(清除)动画输出
# 每一帧会刷新
from IPython.display import display, clear_output
clear_output(wait=True)
9. 疑难问题
多进程
问题:Pycharm可以跑,但需要在jupyter上运行,出现的问题是:程序一直在运行中,但是没有输出结果。
1.jupyter notebook/lab中直接使用多进程的话,虽然在界面上不会报错,但一直会处于运行状态,去查看命令行的话会看到报错:
AttributeError: Can’t get attribute ‘XXX’ on <module ‘main’ >,由于某些原因Pool不能始终使用未在导入的模块中定义的对象。
2.jupyter只能跟踪主进程,没法跟踪子进程? (如何理解)
方法一:
将代码打包成py文件,再让jupyter,导入函数执行。
方法二:
在jupyter里,使用magic函数 %%writefile,并且运行。
调整jupyter 内存大小
$cd .jupyter/$ls
jupyter_lab_config.py lab nbconfig
jupyter_notebook_config.py migrated serverconfig
- 打开Jupyter Notebook并选择New -> Terminal。
- 在终端中输入以下命令:jupyter notebook --generate-config,它将生成一个配置文件jupyter_notebook_config.py。
- 打开配置文件,找到以下代码并取消注释: #c.NotebookApp.max_buffer_size = 100000000 并将数字“100000000”更改为您想要的内存大小。
- 保存并关闭配置文件。
- 重新启动Jupyter Notebook,并您的内存大小就已经更改了。