Google Colab-Magics

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
<marquee style='width: 50%; color: Green;'>Welcome to Tutorialspoint!</marquee>

이제 코드를 실행하면 여기에 표시된대로 스크롤되는 환영 메시지가 화면에 표시됩니다.

다음 코드는 SVG를 문서에 추가합니다.

%%html
<svg xmlns="https://www.w3.org/2000/svg" viewBox="0 0 600 400" width="400" height="400">
   <rect x="10" y="00" width="300" height="100" rx="0" style="fill:orange; stroke:black; fill-opacity:1.0" />
   <rect x="10" y="100" width="300" height="100" rx="0" style="fill:white; stroke:black; fill-opacity:1.0;" />
   <rect x="10" y="200" width="300" height="100" rx="0" style="fill:green; stroke:black; fill-opacity:1.0;" />
</svg>

코드를 실행하면 다음 출력이 표시됩니다.

매직 목록

지원되는 매직의 전체 목록을 얻으려면 다음 명령을 실행하십시오.

%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의 또 다른 강력한 기능을 학습하여 런타임에 프로그램 변수를 설정합니다.