Skip to content

Instantly share code, notes, and snippets.

@minrk
Forked from epifanio/Untitled1.ipynb
Last active November 11, 2022 09:05
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save minrk/11160641 to your computer and use it in GitHub Desktop.
Save minrk/11160641 to your computer and use it in GitHub Desktop.
Display the source blob
Display the rendered blob
Raw
{
"metadata": {
"name": "",
"signature": "sha256:4afb4ed51de9af1fc41d6a8cbb3068e359b6c0c921a1752af34ef19b037d0ef2"
},
"nbformat": 3,
"nbformat_minor": 0,
"worksheets": [
{
"cells": [
{
"cell_type": "code",
"collapsed": false,
"input": [
"# This code can be put in any Python module, it does not require IPython\n",
"# itself to be running already. It only creates the magics subclass but\n",
"# doesn't instantiate it yet.\n",
"from __future__ import print_function\n",
"\n",
"import json\n",
"import os\n",
"import io\n",
"\n",
"from IPython.core.magic import (Magics, magics_class, line_magic,\n",
" cell_magic, line_cell_magic)\n",
"from IPython.core import magic_arguments\n",
"from IPython.core.display import display \n",
"from IPython.utils.py3compat import unicode_type\n",
"from IPython.utils.path import unquote_filename\n",
"\n",
"class JustMetadata(object):\n",
" def __init__(self, metadata):\n",
" self.metadata = metadata\n",
" \n",
" def _repr_json_(self):\n",
" return json.dumps(self.metadata)\n",
"\n",
"# The class MUST call this class decorator at creation time\n",
"@magics_class\n",
"class MyMagics(Magics):\n",
"\n",
" \n",
" @magic_arguments.magic_arguments()\n",
" @magic_arguments.argument(\n",
" '-a', '--append', action='store_true', default=False,\n",
" help='Append contents of the cell to an existing file. '\n",
" 'The file will be created if it does not exist.'\n",
" )\n",
" @magic_arguments.argument(\n",
" 'filename', type=unicode_type,\n",
" help='file to write'\n",
" )\n",
" \n",
" \n",
" @cell_magic\n",
" def writefile2(self, line, cell):\n",
" \"\"\"Write the contents of the cell to a file.\n",
" \"\"\"\n",
" args = magic_arguments.parse_argstring(self.writefile2, line)\n",
" filename = os.path.expanduser(unquote_filename(args.filename))\n",
" \n",
" if os.path.exists(filename):\n",
" print(\"Overwriting %s\" % filename)\n",
" else:\n",
" print(\"Writing %s\" % filename)\n",
" \n",
" mode = 'a' if args.append else 'w'\n",
" with io.open(filename, mode, encoding='utf-8') as f:\n",
" f.write(cell)\n",
" \n",
" display(JustMetadata({'filename' : filename}))\n",
"\n",
"# In order to actually use these magics, you must register them with a\n",
"# running IPython. This code must be placed in a file that is loaded once\n",
"# IPython is up and running:\n",
"ip = get_ipython()\n",
"# You can register the class itself without instantiating it. IPython will\n",
"# call the default constructor on it.\n",
"ip.register_magics(MyMagics)"
],
"language": "python",
"metadata": {},
"outputs": [],
"prompt_number": 10
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"%%writefile2 file2.txt \n",
"line cell cell3"
],
"language": "python",
"metadata": {},
"outputs": [
{
"output_type": "stream",
"stream": "stdout",
"text": [
"Overwriting file2.txt\n"
]
},
{
"json": [
"{\"filename\": \"file2.txt\"}"
],
"metadata": {},
"output_type": "display_data",
"text": [
"<__main__.JustMetadata at 0x110be7b10>"
]
}
],
"prompt_number": 11
}
],
"metadata": {}
}
]
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment