Skip to content

Instantly share code, notes, and snippets.

@damontallen
Last active August 29, 2015 14:18
Show Gist options
  • Save damontallen/73e6d3a5ce692c6d7b92 to your computer and use it in GitHub Desktop.
Save damontallen/73e6d3a5ce692c6d7b92 to your computer and use it in GitHub Desktop.
Using the Save, Load and Run magic commands in an IPython Notebook to demonstrate library development work flow.
Display the source blob
Display the rendered blob
Raw
{
"cells": [
{
"cell_type": "markdown",
"metadata": {},
"source": [
"#Using Load, Save and Run Magic (also Writefile)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"First lets write some code we want to make into a script file."
]
},
{
"cell_type": "code",
"execution_count": 1,
"metadata": {
"collapsed": true
},
"outputs": [],
"source": [
"def fun():\n",
" \"this is a function\"\n",
" print(\"hello World\")"
]
},
{
"cell_type": "code",
"execution_count": 2,
"metadata": {
"collapsed": false
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"hello World\n"
]
}
],
"source": [
"fun()"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"##Save"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Now let's save it to *test.py*. To do that we need to use the [save magic](https://ipython.org/ipython-doc/dev/interactive/magics.html#magic-save) command. In the notebook the format is\n",
"\n",
" %save filename cell_number"
]
},
{
"cell_type": "code",
"execution_count": 3,
"metadata": {
"collapsed": false
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"File `test.py` exists. Overwrite (y/[N])? y\n",
"The following commands were written to file `test.py`:\n",
"def fun():\n",
" \"this is a function\"\n",
" print(\"hello World\")\n"
]
}
],
"source": [
"%save test.py 1"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"_____\n",
"The output you see above is an automatic report about the results of using save. The prompt you see about overwriting is due to this being the second time I ran this notebook."
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"##Load"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"The [load magic](https://ipython.org/ipython-doc/dev/interactive/magics.html#magic-load) allows you to bring in code to your current notebook for editing or execution. You can load an entire file, just specific lines or individual functions from it. Further more you can not only pull code from your local hard drive but also online locations via url.\n",
"\n",
"Below is the results of typing \n",
" \n",
" %load test.py\n",
" \n",
"and pressing Ctrl-Enter."
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": true
},
"outputs": [],
"source": [
"# %load test.py\n",
"def fun():\n",
" \"this is a function\"\n",
" print(\"hello World\")\n"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Notice there is not cell number indicating that that cell has not been executed. Also the magic command is now commented out to prevent accidental repeated execution."
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"##Run"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"I restarted the kernel here to demonstrate the [run magic](https://ipython.org/ipython-doc/dev/interactive/magics.html#magic-run). It allows you to pull code into the current namespace without actually having the code in your notebook."
]
},
{
"cell_type": "code",
"execution_count": 1,
"metadata": {
"collapsed": false
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"hello World again\n"
]
}
],
"source": [
"%run test.py\n",
"fun()"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"By populating your current namespace run is much like the import command. However, unlike when you use import if modifications are made to the code that you brought in using run you do not have to use a different command (like reload) to update your namespace, you just need to execute run again and you are current.\n",
"\n",
"These three commands make developing code libraries in IPython relatively easy. You can now write and test code in your notebook, save it to disk and use it in other notebooks. This is particularly useful when the point of the notebook is not to share code but rather the results."
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"##Writefile"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"The [writefile cell magic](https://ipython.org/ipython-doc/dev/interactive/magics.html#cellmagic-writefile) is more of a companion to the load magic. Each magic is specific to the current cell."
]
},
{
"cell_type": "code",
"execution_count": 2,
"metadata": {
"collapsed": false
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Overwriting test.py\n"
]
}
],
"source": [
"%%writefile test.py\n",
"def fun():\n",
" \"this is a function\"\n",
" print(\"hello World again\")"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"You may notice however there is no prompt about overwriting an existing file."
]
},
{
"cell_type": "code",
"execution_count": 3,
"metadata": {
"collapsed": false
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"hello World again\n"
]
}
],
"source": [
"%run test.py\n",
"fun()"
]
}
],
"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.4.0"
}
},
"nbformat": 4,
"nbformat_minor": 0
}
# coding: utf-8
def fun():
"this is a function"
print("hello World")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment