LaurentRDC / pandoc-plot

Render and include figures in Pandoc documents using your plotting toolkit of choice
https://laurentrdc.github.io/pandoc-plot/
GNU General Public License v2.0
216 stars 8 forks source link

Matplotlib code from file will not render if it contains plt.show in a Python comment #41

Closed mfhepp closed 2 years ago

mfhepp commented 2 years ago

pandoc-plot checks if the code for a Matplotlib graph contains plt.show{.python}, because plt.savefig(...){.python} will fail otherwise. This is correct. Unfortunately, it fails to detect if theplt.show` line will be in a comment, but **only if the source code is imported from a local file.

Works:

import numpy as np
import matplotlib.pyplot as plt
labels = ['A', 'B', 'C']
values = [25, 25, 50]
plt.figure()
plt.pie(values, labels = labels)
# plt.show()

with pie-matplotlib1.py like so:

import numpy as np
import matplotlib.pyplot as plt
labels = ['A', 'B', 'C']
values = [25, 25, 50]
plt.figure()
plt.pie(values, labels = labels)

Does not work:

with pie-matplotlib2.py like so:

import numpy as np
import matplotlib.pyplot as plt
labels = ['A', 'B', 'C']
values = [25, 25, 50]
plt.figure()
plt.pie(values, labels = labels)
# plt.show()
LaurentRDC commented 2 years ago

Yes, this is a compromise between two things:

  1. We don't want plots to silently hang due to plt.show().
  2. I don't want the added complexity of parsing Python source to understand whether plt.show() is reachable or if it is in a comment, for example.

Is there a reason you want to keep this # plt.show() line?

mfhepp commented 2 years ago

Thanks for the swift reply - I can live with as it is; maybe one could add a line to the Mathplotlib section in the documentation and issue the error message a bit more prominently. Maybe insert a Para with the error message in case of any error would also be nice - in some workflows, errors from filters are easy to be missed.

LaurentRDC commented 2 years ago

Ok I've updated the error message to be more specific. It will be part of the next release. Thanks for your report!