Skip to content

Commit

Permalink
modify conversion script to move imports to top
Browse files Browse the repository at this point in the history
  • Loading branch information
rasbt committed Sep 14, 2017
1 parent 074636b commit 9ac9765
Show file tree
Hide file tree
Showing 29 changed files with 390 additions and 1,057 deletions.
19 changes: 13 additions & 6 deletions code/.convert_notebook_to_script.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
import argparse
import os
import subprocess
import textwrap


def convert(input_path, output_path):
Expand All @@ -17,22 +18,28 @@ def convert(input_path, output_path):
def cleanup(path):

skip_lines_startwith = ('Image(filename=',
'# In[',
'# <hr>',
'from IPython.display import Image',
'get_ipython()',
'# <br>',
'from __future__ import print_function')
'# <br>')

clean_content = []
imports = []
with open(path, 'r') as f:
next(f)
next(f)
for line in f:
if line.startswith(skip_lines_startwith):
if line.startswith('from __future__ import print_function'):
clean_content.insert(0,
'from __future__ import '
'print_function\n\n\n')
continue
if line.startswith('import') or (
'from' in line and 'import' in line):
imports.append(line)
else:
clean_content.append(line)

clean_content = ['# coding: utf-8\n\n\n'] + imports + clean_content

with open(path, 'w') as f:
for line in clean_content:
f.write(line)
Expand Down
130 changes: 3 additions & 127 deletions code/ch02/ch02.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -1363,145 +1363,21 @@
},
{
"cell_type": "code",
"execution_count": 9,
"execution_count": 6,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"[NbConvertApp] WARNING | pattern 'input_path' matched no files\n",
"This application is used to convert notebook files (*.ipynb) to various other\n",
"formats.\n",
"\n",
"WARNING: THE COMMANDLINE INTERFACE MAY CHANGE IN FUTURE RELEASES.\n",
"\n",
"Options\n",
"-------\n",
"\n",
"Arguments that take values are actually convenience aliases to full\n",
"Configurables, whose aliases are listed on the help line. For more information\n",
"on full configurables, see '--help-all'.\n",
"\n",
"--debug\n",
" set log level to logging.DEBUG (maximize logging output)\n",
"--generate-config\n",
" generate default config file\n",
"-y\n",
" Answer yes to any questions instead of prompting.\n",
"--execute\n",
" Execute the notebook prior to export.\n",
"--allow-errors\n",
" Continue notebook execution even if one of the cells throws an error and include the error message in the cell output (the default behaviour is to abort conversion). This flag is only relevant if '--execute' was specified, too.\n",
"--stdin\n",
" read a single notebook file from stdin. Write the resulting notebook with default basename 'notebook.*'\n",
"--stdout\n",
" Write notebook output to stdout instead of files.\n",
"--inplace\n",
" Run nbconvert in place, overwriting the existing notebook (only \n",
" relevant when converting to notebook format)\n",
"--no-prompt\n",
" Exclude input and output prompts from converted document.\n",
"--log-level=<Enum> (Application.log_level)\n",
" Default: 30\n",
" Choices: (0, 10, 20, 30, 40, 50, 'DEBUG', 'INFO', 'WARN', 'ERROR', 'CRITICAL')\n",
" Set the log level by value or name.\n",
"--config=<Unicode> (JupyterApp.config_file)\n",
" Default: ''\n",
" Full path of a config file.\n",
"--to=<Unicode> (NbConvertApp.export_format)\n",
" Default: 'html'\n",
" The export format to be used, either one of the built-in formats, or a\n",
" dotted object name that represents the import path for an `Exporter` class\n",
"--template=<Unicode> (TemplateExporter.template_file)\n",
" Default: ''\n",
" Name of the template file to use\n",
"--writer=<DottedObjectName> (NbConvertApp.writer_class)\n",
" Default: 'FilesWriter'\n",
" Writer class used to write the results of the conversion\n",
"--post=<DottedOrNone> (NbConvertApp.postprocessor_class)\n",
" Default: ''\n",
" PostProcessor class used to write the results of the conversion\n",
"--output=<Unicode> (NbConvertApp.output_base)\n",
" Default: ''\n",
" overwrite base name use for output files. can only be used when converting\n",
" one notebook at a time.\n",
"--output-dir=<Unicode> (FilesWriter.build_directory)\n",
" Default: ''\n",
" Directory to write output(s) to. Defaults to output to the directory of each\n",
" notebook. To recover previous default behaviour (outputting to the current\n",
" working directory) use . as the flag value.\n",
"--reveal-prefix=<Unicode> (SlidesExporter.reveal_url_prefix)\n",
" Default: ''\n",
" The URL prefix for reveal.js. This can be a a relative URL for a local copy\n",
" of reveal.js, or point to a CDN.\n",
" For speaker notes to work, a local reveal.js prefix must be used.\n",
"--nbformat=<Enum> (NotebookExporter.nbformat_version)\n",
" Default: 4\n",
" Choices: [1, 2, 3, 4]\n",
" The nbformat version to write. Use this to downgrade notebooks.\n",
"\n",
"To see all available configurables, use `--help-all`\n",
"\n",
"Examples\n",
"--------\n",
"\n",
" The simplest way to use nbconvert is\n",
" \n",
" > jupyter nbconvert mynotebook.ipynb\n",
" \n",
" which will convert mynotebook.ipynb to the default format (probably HTML).\n",
" \n",
" You can specify the export format with `--to`.\n",
" Options include ['asciidoc', 'custom', 'html', 'latex', 'markdown', 'notebook', 'pdf', 'python', 'rst', 'script', 'slides']\n",
" \n",
" > jupyter nbconvert --to latex mynotebook.ipynb\n",
" \n",
" Both HTML and LaTeX support multiple output templates. LaTeX includes\n",
" 'base', 'article' and 'report'. HTML includes 'basic' and 'full'. You\n",
" can specify the flavor of the format used.\n",
" \n",
" > jupyter nbconvert --to html --template basic mynotebook.ipynb\n",
" \n",
" You can also pipe the output to stdout, rather than a file\n",
" \n",
" > jupyter nbconvert mynotebook.ipynb --stdout\n",
" \n",
" PDF is generated via latex\n",
" \n",
" > jupyter nbconvert mynotebook.ipynb --to pdf\n",
" \n",
" You can get (and serve) a Reveal.js-powered slideshow\n",
" \n",
" > jupyter nbconvert myslides.ipynb --to slides --post serve\n",
" \n",
" Multiple notebooks can be given at the command line in a couple of \n",
" different ways:\n",
" \n",
" > jupyter nbconvert notebook*.ipynb\n",
" > jupyter nbconvert notebook1.ipynb notebook2.ipynb\n",
" \n",
" or you can specify the notebooks list in a config file, containing::\n",
" \n",
" c.NbConvertApp.notebooks = [\"my_notebook.ipynb\"]\n",
" \n",
" > jupyter nbconvert --config mycfg.py\n",
"\n"
"[NbConvertApp] Converting notebook ch02.ipynb to script\n",
"[NbConvertApp] Writing 16456 bytes to ch02.py\n"
]
}
],
"source": [
"! python ../.convert_notebook_to_script.py --input ch02.ipynb --output ch02.py"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": true
},
"outputs": [],
"source": []
}
],
"metadata": {
Expand Down
Loading

0 comments on commit 9ac9765

Please sign in to comment.