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

deferred loading breaks plotly in HTML output #39

Closed markwort closed 2 years ago

markwort commented 2 years ago

Dear @LaurentRDC ,

first of all, thank you for this filter, it allowed me to include some nice plotly figures in my Master's thesis. I really like having things that can be rebuilt on demand when some data changes etc., so this filter was really helpful.

Now I'm in the middle of preparing my defense and I thought I might try an HTML/JS slideshow instead of PDF, which would grant the ability to navigate the plots during the presentation.

My first attempts showed no errors, but no plots either, but I've been able to narrow the issue down. Apparently, the plotly JS doesn't like being loaded deferred, at least I can only see plotly figures when I remove all defer statements from the html file pandoc produces.

I've attached a minimal reproducer (which additionally shows that for bokeh, deferred loading of the library works flawless). I'm not sure what would be the best option to solve this issue, perhaps making the deferred loading optional and disable it by default for plotly figures?

Here's the minimal reproducer:

---
title: pandoc-plot reproducer
...

# section foo

## plotly foo

```{.python .plotly_python format=html caption="sample plotly fig from https://plotly.com/python/line-and-scatter/"}
import plotly.graph_objects as go
import numpy as np

N = 1000
t = np.linspace(0, 10, 100)
y = np.sin(t)

fig = go.Figure(data=go.Scatter(x=t, y=y, mode='markers'))

bokeh foo

import numpy as np

from bokeh.plotting import figure
from bokeh.transform import linear_cmap
from bokeh.util.hex import hexbin

np.random.seed(23)

n = 50000
x = np.random.standard_normal(n)
y = np.random.standard_normal(n)

bins = hexbin(x, y, 0.1)

p = figure(title="Interactive plotting with Bokeh", tools="wheel_zoom,pan,reset", match_aspect=True, background_fill_color='#440154', plot_width=550, plot_height=550)

p.grid.visible = False

p.hex_tile(q="q", r="r", size=0.1, line_color=None, source=bins,
           fill_color=linear_cmap('counts', 'Viridis256', 0, max(bins.counts)))

Version numbers involved:

[julian@fedora pandoc_plot_reproducer]$ pandoc -v pandoc 2.14.0.3 Compiled with pandoc-types 1.22, texmath 0.12.3, skylighting 0.10.5.2, citeproc 0.4.0.1, ipynb 0.1.0.1 User data directory: /home/julian/.local/share/pandoc Copyright (C) 2006-2021 John MacFarlane. Web: https://pandoc.org This is free software; see the source for copying conditions. There is no warranty, not even for merchantability or fitness for a particular purpose. [julian@fedora pandoc_plot_reproducer]$ pandoc-plot -v 1.4.0 [julian@fedora pandoc_plot_reproducer]$ pip freeze | grep -i -e plotly -e bokeh bokeh==2.4.2 plotly==5.6.0


How I built it:

pandoc --filter pandoc-plot test.md --standalone -o test.html


Testing:

firefox test.html chromium-browser test.html


Attempted fix:

sed 's/defer></></g' test.html > test_no_defer.html firefox test_no_defer.html chromium-browser test_no_defer.html



Of course this `sed` call above is perhaps prone to failure, if anyone reads this in the future and thinks this is a good workaround.

This is what I get as the baseline:
![image](https://user-images.githubusercontent.com/38984608/156735983-c1b6294c-eaec-400c-a4c8-7fec77f95700.png)

Here's the "fixed" version without deferring:
![image](https://user-images.githubusercontent.com/38984608/156736056-9ef6f934-e9b6-4549-b1df-78ae94a3aa97.png)

Please let me know if there is anything else I can try or any way to help.
Despite having had to learn some Haskell in the second year of university, I could never wrap my head around it, so I fear that I can't solve this directly...

Best regards
Julian
LaurentRDC commented 2 years ago

Hi Julian,

Thank you for your thorough report. I've pushed a commit to disable this deferring behavior. I thought there might be a performance gain, but I'd rather have interactive plots load at all than load fast, some of the times.

If the tests pass, I'll be releasing version 1.4.1 today. I'll post here again when it's released.

Laurent

LaurentRDC commented 2 years ago

Ok pandoc-plot 1.4.1 has been released. You can already install it from Hackage. Give GitHub a few hours to produce executables that you can download.

Let me know if things have been fixed for you

markwort commented 2 years ago

Thanks for your quick fix! It seems to work as expected now with version 1.4.1.