Fixed PDF generation re %matplotlib magic

Fixed the scripts that comment out the matplotlib magic.
It was commenting out all of the magics - the first one, and
ones in markdown cells. It should only comment out the ones
in code cells, and leave the first one.
This commit is contained in:
Roger Labbe 2017-01-18 12:23:06 -08:00
parent 91e9e08377
commit c97186c8b4
4 changed files with 29 additions and 14 deletions

View File

@ -1,5 +1,5 @@
rem call run_notebooks
rem cd ..
call run_notebooks
cd ..
ipython merge_book.py
jupyter nbconvert --to latex --template book book.ipynb

View File

@ -14,3 +14,4 @@ rm --f chapter.pdf
rmdir /S /Q book_files
rmdir /S /Q chapter_files
rmdir /S /Q tmp

View File

@ -1,15 +1,29 @@
import os, sys
import os, sys, io
print(sys.argv[1])
infile = open(sys.argv[1], encoding="utf8")
data = infile.read()
infile.close()
outfile = open(sys.argv[1], "w", encoding="utf8")
data = data.replace("%matplotlib notebook", "#%matplotlib notebook")
data = data.replace("%matplotlib inline #reset", "#%matplotlib inline #reset")
data = data.replace("time.sleep", "#time.sleep")
data = data.replace("fig.canvas.draw", "#fig.canvas.draw")
data = data.replace("plt.gcf().canvas.draw", "#plt.gcf().canvas.draw")
buf = io.StringIO(data)
first_inline = False
is_code_cell = False
for line in buf:
# can't comment out first inline that is always in the first cell
if '"cell_type":' in line:
is_code_cell = '"code"' in line
if is_code_cell:
if '%matplotlib inline' in line:
if first_inline:
line = line.replace("%matplotlib inline", "#%matplotlib inline")
first_inline = True
line = line.replace("%matplotlib notebook", "#%matplotlib notebook")
line = line.replace("time.sleep", "#time.sleep")
line = line.replace("fig.canvas.draw", "#fig.canvas.draw")
line = line.replace("plt.gcf().canvas.draw", "#plt.gcf().canvas.draw")
outfile.write(line)
outfile.write(data)
outfile.close()

View File

@ -1,10 +1,10 @@
mkdir tmp
REM copy ..\*.ipynb .\tmp
REM copy ..\*.py .\tmp
REM cp -r ..\code\ .\tmp\code\
copy ..\*.ipynb .\tmp
copy ..\*.py .\tmp
cp -r ..\code\ .\tmp\code\
cd tmp
REM forfiles /m *.ipynb /c "cmd /c ipython ..\rm_notebook.py @file"
REM forfiles /m *.ipynb /c "cmd /c jupyter nbconvert --to notebook --execute @file --output @file"
forfiles /m *.ipynb /c "cmd /c ipython ..\rm_notebook.py @file"
forfiles /m *.ipynb /c "cmd /c jupyter nbconvert --to notebook --execute @file --output @file"