From 6c0a5706c3766999841e8747bb78b78062cf5b6a Mon Sep 17 00:00:00 2001 From: Rishabh Raj Date: Sun, 13 Apr 2014 20:49:46 +0530 Subject: [PATCH] Remove sample.ipynb, template now inline --- doc/ext/notebook.py | 43 ++++++++++++++++--- .../applications/notebook/sample.ipynb | 25 ----------- .../auto_examples/notebook/sample.ipynb | 25 ----------- 3 files changed, 38 insertions(+), 55 deletions(-) delete mode 100644 doc/source/auto_examples/applications/notebook/sample.ipynb delete mode 100644 doc/source/auto_examples/notebook/sample.ipynb diff --git a/doc/ext/notebook.py b/doc/ext/notebook.py index 210c6878..6507020c 100644 --- a/doc/ext/notebook.py +++ b/doc/ext/notebook.py @@ -3,6 +3,31 @@ __all__ = ['python_to_notebook'] import json import copy +sample = """{ + "metadata": { + "name":"" + }, + "nbformat": 3, + "nbformat_minor": 0, + "worksheets": [ + { + "cells": [ + { + "cell_type": "code", + "collapsed": false, + "input": [ + "%matplotlib inline" + ], + "language": "python", + "metadata": {}, + "outputs": [] + } + ], + "metadata": {} + } + ] +}""" + class Notebook(): """ @@ -33,14 +58,21 @@ class Notebook(): ] } - self.cell_type = {'input_code': self.cell_code, 'input_markdown': self.cell_md} - with open(sample_notebook_path, 'r') as sample, open(example_file, 'r') as pythonfile: - self.template = json.load(sample) + self.cell_type = {'input': self.cell_code, 'source': self.cell_md} + self.keys = {'input_code': 'input', 'input_markdown': 'source'} + with open(example_file, 'r') as pythonfile: + self.template = json.loads(sample) self.code = pythonfile.readlines() # Adds an extra newline at the end, # this aids in extraction of text segments self.code.append('\n') + def fetchkey(self, type_of_value): + """ Returns the key required for insertion into notebook, + based on the type of value. + """ + return self.keys[type_of_value] + def filter_continuous_duplication(self): """ Clusters multiple '\n's into one. For ex - 'import xyz\n\n\n print 2' becomes 'import xyz\n print 2' """ @@ -53,8 +85,9 @@ class Notebook(): Cell differs with type of value. """ if type_of_value in ['input_markdown', 'input_code']: - self.template["worksheets"][0]["cells"].append(copy.deepcopy(self.cell_type[type_of_value])) - self.template["worksheets"][0]["cells"][segment_number][type_of_value] = value + key = self.fetchkey(type_of_value) + self.template["worksheets"][0]["cells"].append(copy.deepcopy(self.cell_type[key])) + self.template["worksheets"][0]["cells"][segment_number][key] = value def json(self, notebook_path): """ Writes the template to file (json) """ diff --git a/doc/source/auto_examples/applications/notebook/sample.ipynb b/doc/source/auto_examples/applications/notebook/sample.ipynb deleted file mode 100644 index fac5f53e..00000000 --- a/doc/source/auto_examples/applications/notebook/sample.ipynb +++ /dev/null @@ -1,25 +0,0 @@ -{ - "metadata": { - "name":"" - }, - "nbformat": 3, - "nbformat_minor": 0, - "worksheets": [ - { - "cells": [ - { - "cell_type": "code", - "collapsed": false, - "input": [ - "%matplotlib inline" - ], - "language": "python", - "metadata": {}, - "outputs": [] - } - ], - "metadata": {} - } - ] -} - diff --git a/doc/source/auto_examples/notebook/sample.ipynb b/doc/source/auto_examples/notebook/sample.ipynb deleted file mode 100644 index fac5f53e..00000000 --- a/doc/source/auto_examples/notebook/sample.ipynb +++ /dev/null @@ -1,25 +0,0 @@ -{ - "metadata": { - "name":"" - }, - "nbformat": 3, - "nbformat_minor": 0, - "worksheets": [ - { - "cells": [ - { - "cell_type": "code", - "collapsed": false, - "input": [ - "%matplotlib inline" - ], - "language": "python", - "metadata": {}, - "outputs": [] - } - ], - "metadata": {} - } - ] -} -