查看原文
其他

玩转Jupyter Notebook3-(“魔法”命令篇)

pythonic生物人 pythonic生物人 2022-09-11

"pythonic生物人"的第96篇分享

本文梳理Jupyter Notebook中“Magics”命令。

本文速览

1、魔法命令介绍
  %lsmagic:列出所有magics命令 
  %quickref:输出所有魔法指令的简单版帮助文档
  %Magics_Name?:输出某个魔法命令详细帮助文档 
2、Line magics:Line魔法指令
  %alias:设置指令的别名
  %conda:cell中安装package
  %dhist:输出历史访问目录
  %history:列出历史输入的指令
  %magic:输出所有魔法指令帮助文档
  %matplotlib inline:效果等价于plt.show()
  %notebook:导出当前notebook所有历史输入到一个文件中
  %pip:在cell中使用pip指令
  %pwd:输出当前路径
  %pycat:预览文件,类似linux中cat
  %run:执行脚本
  %time:执行时间
3、Cell magics:Cell魔法指令
  %%writefile:将当前cell中内容写入文件中
  %%latex:写Latex公式
  %%script :写bash、perl、javascript、js 等命令
   写bash程序
   写perl程序
   写python2程序 

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  %xmode
Available 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
  • %quickref:输出所有魔法指令的简单版帮助文档

  • %Magics_Name?:输出某个魔法命令详细帮助文档

魔法命令名称?输出魔法命令的详细帮助文档,以%alias为例:


2、Line magics:Line魔法指令

  • %alias:设置指令的别名

windows有8个默认的指令,功能和linux下一样。

#Windows下有8个命令
In [1]: %alias#
Total number of aliases: 8
Out[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]: %alias
Total number of aliases: 16
Out[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中安装package

%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程序

In [9]: %%script bash
   ...: for i in 1 2 3; do
   ...: echo $i;
   ...: done
1
2
3

写perl程序

In [11]: %%script perl
    ...: print "hhn";
hhn

写python2程序

In [12]: %%script python2
    ...: print "hhhn"
hhhn

参考资料:https://ipython.readthedocs.io/en/stable/interactive/magics.html#

您可能也对以下帖子感兴趣

文章有问题?点此查看未经处理的缓存