📜  Google Colab-魔术

📅  最后修改于: 2020-10-16 02:36:42             🧑  作者: Mango


Magics是一组系统命令,它们提供了一种迷你扩展命令语言。

魔术有两种类型-

  • 线魔术

  • 细胞魔法

换行符的名称表示它由单行命令组成,而单元魔术符覆盖代码单元的整个主体。

在使用线魔术的情况下,该命令前面带有一个%字符,在使用单元魔术的情况下,该命令前面带有两个%字符(%%)。

让我们看一下这两个例子。

线魔术

在代码单元格中键入以下代码-

%ldir

您将看到本地目录的内容,如下所示-

drwxr-xr-x 3 root 4096 Jun 20 10:05 drive/
drwxr-xr-x 1 root 4096 May 31 16:17 sample_data/

尝试以下命令-

%history

这显示了您先前执行的命令的完整历史记录。

细胞魔法

在您的代码单元格中键入以下代码-

%%html
Welcome to Tutorialspoint!

现在,如果运行代码,您将在屏幕上看到滚动欢迎消息,如下所示:

细胞魔法

以下代码会将SVG添加到您的文档中。

%%html

   
   
   

如果运行代码,您将看到以下输出-

细胞魔法输出

魔法清单

要获取受支持的魔术的完整列表,请执行以下命令-

%lsmagic

您将看到以下输出-

Available line magics:
%alias %alias_magic %autocall %automagic %autosave %bookmark %cat %cd %clear
%colors %config %connect_info %cp %debug %dhist %dirs %doctest_mode %ed %edit
%env %gui %hist %history %killbgscripts %ldir %less %lf %lk %ll %load %load_ext
%loadpy %logoff %logon %logstart %logstate %logstop %ls %lsmagic %lx %macro
%magic %man %matplotlib %mkdir %more %mv %notebook %page %pastebin %pdb %pdef
%pdoc %pfile %pinfo %pinfo2 %pip %popd %pprint %precision %profile %prun
%psearch %psource %pushd %pwd %pycat %pylab %qtconsole %quickref %recall
%rehashx %reload_ext %rep %rerun %reset %reset_selective %rm %rmdir %run %save
%sc %set_env %shell %store %sx %system %tb %tensorflow_version %time %timeit
%unalias %unload_ext %who %who_ls %whos %xdel %xmode

Available cell magics:
%%! %%HTML %%SVG %%bash %%bigquery %%capture %%debug %%file %%html %%javascript
%%js %%latex %%perl %%prun %%pypy %%python %%python2 %%python3 %%ruby %%script
%%sh %%shell %%svg %%sx %%system %%time %%timeit %%writefile

Automagic is ON, % prefix IS NOT needed for line magics.

接下来,您将学习Colab的另一项强大功能,即在运行时设置程序变量。