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:
@@ -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()
|
||||
|
||||
Reference in New Issue
Block a user