Skip to content

Instantly share code, notes, and snippets.

@tonyfast
Last active May 28, 2021 20:48
Show Gist options
  • Save tonyfast/3440a5b4c19a257d6164fff93fbef154 to your computer and use it in GitHub Desktop.
Save tonyfast/3440a5b4c19a257d6164fff93fbef154 to your computer and use it in GitHub Desktop.
Display the source blob
Display the rendered blob
Raw
{
"cells": [
{
"cell_type": "markdown",
"id": "a4505e0f",
"metadata": {},
"source": [
"# write a document that uses line and/or cell magics"
]
},
{
"cell_type": "markdown",
"id": "3795a327",
"metadata": {},
"source": [
"below is a list of line and cell magics"
]
},
{
"cell_type": "code",
"execution_count": 13,
"id": "2dbc35e0",
"metadata": {},
"outputs": [],
"source": [
" a = [210, 20]"
]
},
{
"cell_type": "code",
"execution_count": 8,
"id": "25528c24",
"metadata": {},
"outputs": [],
"source": [
" def f():\n",
" import time\n",
" time.sleep(2)"
]
},
{
"cell_type": "code",
"execution_count": 1,
"id": "ce81e518",
"metadata": {},
"outputs": [
{
"data": {
"application/json": {
"cell": {
"!": "OSMagics",
"HTML": "Other",
"SVG": "Other",
"bash": "Other",
"capture": "ExecutionMagics",
"debug": "ExecutionMagics",
"file": "Other",
"html": "DisplayMagics",
"javascript": "DisplayMagics",
"js": "DisplayMagics",
"latex": "DisplayMagics",
"markdown": "DisplayMagics",
"perl": "Other",
"prun": "ExecutionMagics",
"pypy": "Other",
"python": "Other",
"python2": "Other",
"python3": "Other",
"ruby": "Other",
"script": "ScriptMagics",
"sh": "Other",
"svg": "DisplayMagics",
"sx": "OSMagics",
"system": "OSMagics",
"time": "ExecutionMagics",
"timeit": "ExecutionMagics",
"writefile": "OSMagics"
},
"line": {
"alias": "OSMagics",
"alias_magic": "BasicMagics",
"autoawait": "AsyncMagics",
"autocall": "AutoMagics",
"automagic": "AutoMagics",
"autosave": "KernelMagics",
"bookmark": "OSMagics",
"cat": "Other",
"cd": "OSMagics",
"clear": "KernelMagics",
"colors": "BasicMagics",
"conda": "PackagingMagics",
"config": "ConfigMagics",
"connect_info": "KernelMagics",
"cp": "Other",
"debug": "ExecutionMagics",
"dhist": "OSMagics",
"dirs": "OSMagics",
"doctest_mode": "BasicMagics",
"ed": "Other",
"edit": "KernelMagics",
"env": "OSMagics",
"gui": "BasicMagics",
"hist": "Other",
"history": "HistoryMagics",
"killbgscripts": "ScriptMagics",
"ldir": "Other",
"less": "KernelMagics",
"lf": "Other",
"lk": "Other",
"ll": "Other",
"load": "CodeMagics",
"load_ext": "ExtensionMagics",
"loadpy": "CodeMagics",
"logoff": "LoggingMagics",
"logon": "LoggingMagics",
"logstart": "LoggingMagics",
"logstate": "LoggingMagics",
"logstop": "LoggingMagics",
"ls": "Other",
"lsmagic": "BasicMagics",
"lx": "Other",
"macro": "ExecutionMagics",
"magic": "BasicMagics",
"man": "KernelMagics",
"matplotlib": "PylabMagics",
"mkdir": "Other",
"more": "KernelMagics",
"mv": "Other",
"notebook": "BasicMagics",
"page": "BasicMagics",
"pastebin": "CodeMagics",
"pdb": "ExecutionMagics",
"pdef": "NamespaceMagics",
"pdoc": "NamespaceMagics",
"pfile": "NamespaceMagics",
"pinfo": "NamespaceMagics",
"pinfo2": "NamespaceMagics",
"pip": "PackagingMagics",
"popd": "OSMagics",
"pprint": "BasicMagics",
"precision": "BasicMagics",
"prun": "ExecutionMagics",
"psearch": "NamespaceMagics",
"psource": "NamespaceMagics",
"pushd": "OSMagics",
"pwd": "OSMagics",
"pycat": "OSMagics",
"pylab": "PylabMagics",
"qtconsole": "KernelMagics",
"quickref": "BasicMagics",
"recall": "HistoryMagics",
"rehashx": "OSMagics",
"reload_ext": "ExtensionMagics",
"rep": "Other",
"rerun": "HistoryMagics",
"reset": "NamespaceMagics",
"reset_selective": "NamespaceMagics",
"rm": "Other",
"rmdir": "Other",
"run": "ExecutionMagics",
"save": "CodeMagics",
"sc": "OSMagics",
"set_env": "OSMagics",
"store": "StoreMagics",
"sx": "OSMagics",
"system": "OSMagics",
"tb": "ExecutionMagics",
"time": "ExecutionMagics",
"timeit": "ExecutionMagics",
"unalias": "OSMagics",
"unload_ext": "ExtensionMagics",
"who": "NamespaceMagics",
"who_ls": "NamespaceMagics",
"whos": "NamespaceMagics",
"xdel": "NamespaceMagics",
"xmode": "BasicMagics"
}
},
"text/plain": [
"Available line magics:\n",
"%alias %alias_magic %autoawait %autocall %automagic %autosave %bookmark %cat %cd %clear %colors %conda %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 %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 %store %sx %system %tb %time %timeit %unalias %unload_ext %who %who_ls %whos %xdel %xmode\n",
"\n",
"Available cell magics:\n",
"%%! %%HTML %%SVG %%bash %%capture %%debug %%file %%html %%javascript %%js %%latex %%markdown %%perl %%prun %%pypy %%python %%python2 %%python3 %%ruby %%script %%sh %%svg %%sx %%system %%time %%timeit %%writefile\n",
"\n",
"Automagic is ON, % prefix IS NOT needed for line magics."
]
},
"execution_count": 1,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"%lsmagic"
]
},
{
"cell_type": "code",
"execution_count": 14,
"id": "3853c913",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Stored 'a' (list)\n"
]
}
],
"source": [
" %store a"
]
},
{
"cell_type": "code",
"execution_count": 11,
"id": "0952d640",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"CPU times: user 2.33 ms, sys: 149 µs, total: 2.48 ms\n",
"Wall time: 2 s\n"
]
}
],
"source": [
" %time f()"
]
},
{
"cell_type": "code",
"execution_count": 10,
"id": "acfc2cb8",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"2 s ± 154 µs per loop (mean ± std. dev. of 7 runs, 1 loop each)\n"
]
}
],
"source": [
" %timeit f()"
]
},
{
"cell_type": "code",
"execution_count": 15,
"id": "fbd40e5e",
"metadata": {},
"outputs": [
{
"data": {
"text/markdown": [
"# Hi\n"
],
"text/plain": [
"<IPython.core.display.Markdown object>"
]
},
"metadata": {},
"output_type": "display_data"
}
],
"source": [
" %%markdown\n",
" # Hi"
]
},
{
"cell_type": "code",
"execution_count": 16,
"id": "0cac60b3",
"metadata": {},
"outputs": [
{
"data": {
"text/html": [
"<h1 id=\"x\">Hello</h1>\n",
"<style>\n",
"#x {color: red};\n",
"</style>\n"
],
"text/plain": [
"<IPython.core.display.HTML object>"
]
},
"metadata": {},
"output_type": "display_data"
}
],
"source": [
" %%html\n",
" <h1 id=\"x\">Hello</h1>\n",
" <style>\n",
" #x {color: red};\n",
" </style>"
]
},
{
"cell_type": "code",
"execution_count": 17,
"id": "ce62402b",
"metadata": {},
"outputs": [
{
"data": {
"image/svg+xml": [
"<svg height=\"110\" width=\"400\">\n",
" <rect height=\"100\" style=\"fill:rgb(0,0,255);stroke-width:3;stroke:rgb(0,0,0)\" width=\"300\"/>\n",
" Sorry, your browser does not support inline SVG. \n",
"</svg>"
],
"text/plain": [
"<IPython.core.display.SVG object>"
]
},
"metadata": {},
"output_type": "display_data"
}
],
"source": [
" %%svg\n",
" \n",
"<svg width=\"400\" height=\"110\">\n",
" <rect width=\"300\" height=\"100\" style=\"fill:rgb(0,0,255);stroke-width:3;stroke:rgb(0,0,0)\" />\n",
" Sorry, your browser does not support inline SVG. \n",
"</svg>"
]
},
{
"cell_type": "code",
"execution_count": 18,
"id": "87f7c693",
"metadata": {},
"outputs": [],
"source": [
" v = !echo \"hello world\""
]
},
{
"cell_type": "code",
"execution_count": 19,
"id": "fa63c1ed",
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"['hello world']"
]
},
"execution_count": 19,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
" v"
]
},
{
"cell_type": "code",
"execution_count": 20,
"id": "467945d7",
"metadata": {},
"outputs": [],
"source": [
" init = !git init"
]
},
{
"cell_type": "code",
"execution_count": 22,
"id": "11e51325",
"metadata": {},
"outputs": [],
"source": [
" name = \"y'all\""
]
},
{
"cell_type": "code",
"execution_count": 23,
"id": "3b0f2329",
"metadata": {},
"outputs": [],
"source": [
" v = !echo \"hello $name\""
]
},
{
"cell_type": "code",
"execution_count": 24,
"id": "a3a8ef7a",
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"[\"hello y'all\"]"
]
},
"execution_count": 24,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
" v"
]
},
{
"cell_type": "code",
"execution_count": 25,
"id": "3b43b2b1",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"\u001b[38;5;242m-\u001b[m Creating gist magics.ipynb\n",
"\u001b[0;32m✓\u001b[0m Created gist magics.ipynb\n",
"https://gist.github.com/3440a5b4c19a257d6164fff93fbef154\n"
]
}
],
"source": [
" !gh gist create magics.ipynb"
]
},
{
"cell_type": "code",
"execution_count": 26,
"id": "0500e29e",
"metadata": {},
"outputs": [],
"source": [
" shell = get_ipython()"
]
},
{
"cell_type": "code",
"execution_count": 31,
"id": "7b344332",
"metadata": {},
"outputs": [],
"source": [
" def my_magic(line, cell=None):\n",
" print(F\"the line is: {line}\")\n",
" cell and print(F\"the cell is: {cell}\")"
]
},
{
"cell_type": "code",
"execution_count": 34,
"id": "e5000aff",
"metadata": {},
"outputs": [],
"source": [
" shell.register_magic_function(my_magic, \"line_cell\")"
]
},
{
"cell_type": "code",
"execution_count": 35,
"id": "62eba45a",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"the line is: \n",
"the cell is: this barfs\n",
"\n"
]
}
],
"source": [
" %%my_magic \n",
" this barfs"
]
},
{
"cell_type": "code",
"execution_count": 36,
"id": "a17a1a64",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"the line is: the line input\n",
"the cell is: this barfs\n",
"\n"
]
}
],
"source": [
" %%my_magic the line input\n",
" this barfs"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "e77e8a97",
"metadata": {},
"outputs": [],
"source": []
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 3",
"language": "python",
"name": "python3"
},
"language_info": {
"codemirror_mode": {
"name": "ipython",
"version": 3
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.7.7"
}
},
"nbformat": 4,
"nbformat_minor": 5
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment