本篇文章梳理Jupyter Notebook中“Magics”命令,文中详细的为大家讲解了Jupyter Notebook3魔法命令以及示例代码,有需要的朋友可以借鉴参考下,希望可以有所帮助,祝大家多多进步
目录
1、魔法命令介绍
%lsmagic:列出所有magics命令
%quickref:输出所有魔法指令的简单版帮助文档
%Magics_Name?:输出某个魔法命令详细帮助文档
2、Line magics:Line魔法指令
3、Cell magics:Cell魔法指令
写bash程序
写perl程序
1、魔法命令介绍
%lsmagic:列出所有magics命令
Available line magics:【对当前行使用共计93个】%alias %alias_magic %autoawait %autocall %automagic %autosave %bookmark %cd %clear %cls %colors %conda %config %connect_info %copy %ddir %debug %dhist %dirs %doctest_mode %echo %ed %edit %env %gui %hist %history %killbgscripts %ldir %less %load %load_ext %loadpy %logoff %logon %logstart %logstate %logstop %ls %lsmagic %macro %magic %matplotlib %mkdir %more %notebook %page %pastebin %pdb %pdef %pdoc %pfile %pinfo %pinfo2 %pip %popd %pprint %precision %prun %psearch %psource %pushd %pwd %pycat %pylab %qtconsole %quickref %recall %rehashx %reload_ext %ren %rep %rerun %reset %reset_selective %rmdir %run %save %sc %set_env %store %sx %system %tb %time %timeit %unalias %unload_ext %who %who_ls %whos %xdel %xmodeAvailable cell magics:【对当前cell使用共计28个】%%! %%HTML %%SVG %%bash %%capture %%cmd %%debug %%file %%html %%javascript %%js %%latex %%markdown %%perl %%prun %%pypy %%python %%python2 %%python3 %%ruby %%script %%sh %%svg %%sx %%system %%time %%timeit %%writefile```](https://img.php.cn/upload/image/677/962/552/1649475819677903.png)%quickref:输出所有魔法指令的简单版帮助文档%Magics_Name?:输出某个魔法命令详细帮助文档魔法命令名称?输出魔法命令的详细帮助文档,以%alias为例:2、Line magics:Line魔法指令%alias:设置指令的别名windows有8个默认的指令,功能和linux下一样。```asp#Windows下有8个命令In [1]: %alias#Total number of aliases: 8Out[1]:[('copy', 'copy'),('ddir', 'dir /ad /on'),('echo', 'echo'),('ldir', 'dir /ad /on'),#列出文件夹('ls', 'dir /on'),('mkdir', 'mkdir'),#创建文件夹('ren', 'ren'),('rmdir', 'rmdir')]#删除文件夹
Linux下有16个默认指令,感兴趣可自己试验。
In [3]: %aliasTotal number of aliases: 16Out[3]:[('cat', 'cat'),('clear', 'clear'),('cp', 'cp'),('ldir', 'ls -F -o --color %l | grep /$'),('less', 'less'),('lf', 'ls -F -o --color %l | grep ^-'),('lk', 'ls -F -o --color %l | grep ^l'),('ll', 'ls -F -o --color'),('ls', 'ls -F --color'),('lx', 'ls -F -o --color %l | grep ^-..x'),('man', 'man'),('mkdir', 'mkdir'),('more', 'more'),('mv', 'mv'),('rm', 'rm'),('rmdir', 'rmdir')]
自己设置指令的别名,个人感觉没啥意义,介绍一个。

%conda:cell中安装packageM
%conda install package_names
%dhist:输出历史访问目录
%history:列出历史输入的指令
效果类似linux中history。

%magic:输出所有魔法指令帮助文档
%matplotlib inline:效果等价于plt.show()
%notebook:导出当前notebook所有历史输入到一个文件中
%notebook notebook.ipynb将所有历史输入导入notebook.ipynb文件中
%pip:在cell中使用pip指令
%pwd:输出当前路径
%pycat:预览文件,类似linux中cat
%run:执行脚本

%time:执行时间

3、Cell magics:Cell魔法指令
%%writefile:将当前cell中内容写入文件中

%%latex:写Latex公式
%%latex\begin{equation}\int_0^\infty \frac{x^3}{e^x-1}\,dx = \frac{\pi^4}{15}\label{eq:sample}\end{equation}

%%script :写bash、perl、javascript、js 等命令
这个可以使用bash、perl、javascript、js 等等,不过经过测试,在jupyter notebook中不友好,在ipython中没什么问题。
以下在ipython中完成:
写bash程序```asp
In [9]: %%script bash
…: for i in 1 2 3; do
…: echo $i;
…: done
1
2
3
写perl程序```aspIn [11]: %%script perl...: print "hhn";hhn
写python2程序
In [12]: %%script python2...: print "hhhn"...:...:hhhn
参考资料:https://ipython.readthedocs.io/en/stable/interactive/magics.html#
以上就是JupyterNotebook3魔法命令详解及示例的详细内容。
Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号