jupyter magic commands

/

jupyter magic commandsの使い方

タグ


概要

  • pythonのプログラムとは別にjupyter独自のコマンドがある
  • セルの先頭に%, %%をつけることで実行できる

代表的なマジックコマンドの使用例

%%time, %time

  • %time
    • 次の行の実行時間を測定する
  • %%time
    • セル全体の実行時間を測定する

%env

  • 環境変数を設定する
%env OPENAI_API_KEY=xxxxxxxxxxxxxxxxxxxx

%%writefile <output-name>

  • セルに記述するとセルの内容がファイルとして出力される
  • e.g.
    • %% writefile a.py
      • a.pyで出力する

%%bash

  • セルの最初に記述するとセルがshell scriptになる

%watermark

  • 実行しているos, マシンスペック, python, jupyterの環境を表示するmagic
%pip install watermark # 必要ならば
%load_ext watermark
%watermark
Python implementation: CPython
Python version       : 3.10.12
IPython version      : 7.34.0

Compiler    : GCC 11.4.0
OS          : Linux
Release     : 6.1.58+
Machine     : x86_64
Processor   : x86_64
CPU cores   : 2
Architecture: 64bit

%autoreload

  • 自作したpythonモジュール等をimportたびに更新するmagic
%load_ext autoreload
%autoreload 2

%%html

  • セルの内容をHTMLとして表示する
  • iframeを埋め込むことができる
%%html
<iframe src="sample.html" width="800" height="600"></iframe>