Skip to content

Instantly share code, notes, and snippets.

@epifanio
Created April 21, 2014 23:55
Show Gist options
  • Save epifanio/11160607 to your computer and use it in GitHub Desktop.
Save epifanio/11160607 to your computer and use it in GitHub Desktop.
Display the source blob
Display the rendered blob
Raw
{
"worksheets": [
{
"cells": [
{
"metadata": {},
"cell_type": "code",
"input": "#display(myobj, metadata={'key' : 'value'})",
"prompt_number": 16,
"outputs": [],
"language": "python",
"trusted": true,
"collapsed": false
},
{
"metadata": {},
"cell_type": "code",
"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.\nfrom __future__ import print_function\nfrom IPython.core.magic import (Magics, magics_class, line_magic,\n cell_magic, line_cell_magic)\nfrom IPython.core import magic_arguments\nfrom IPython.core import display \nfrom IPython.utils.py3compat import unicode_type\nfrom IPython.utils.path import unquote_filename\nimport os\nimport io\n\n# The class MUST call this class decorator at creation time\n@magics_class\nclass MyMagics(Magics):\n\n def _repr_javascript_(self): \n return ('', self.metadata)\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 @cell_magic\n def cmagic(self, line, cell):\n \"my cell magic\"\n ob = self._repr_javascript_()\n return line, cell\n\n#display(myobj, metadata={'key' : 'value'})\n\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:\nip = get_ipython()\n# You can register the class itself without instantiating it. IPython will\n# call the default constructor on it.\nip.register_magics(MyMagics)",
"prompt_number": 13,
"outputs": [],
"language": "python",
"trusted": true,
"collapsed": false
},
{
"metadata": {},
"cell_type": "code",
"input": "%%writefile2 file2.txt \nline cell cell3",
"prompt_number": 14,
"outputs": [
{
"output_type": "stream",
"text": "Writing file2.txt\n",
"stream": "stdout"
}
],
"language": "python",
"trusted": true,
"collapsed": false
},
{
"metadata": {},
"cell_type": "code",
"input": "",
"outputs": [],
"language": "python",
"trusted": true,
"collapsed": false
}
],
"metadata": {}
}
],
"metadata": {
"name": "",
"signature": "sha256:d107612f7bb4c7020734fdd5ed5efb052af4c7d273799706148f60894d513aa6"
},
"nbformat": 3
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment