Adding plotly figures to mne.Report()

Dear everybody,

has anybody here ever tried to add .htmls generated with plotly to mne.Report()?

plotly offers a convenient way of saving interactive plots to html via figure.write_html(“foo.html”).

I tried adding the interactive plot as a figure via:

  • report.add_figs_to_section(figure) → doesn’t work, as mne tries to render a 3d figure and fails

and

  • report.add_htmls_to_section(“foo.hmtl”) → doesn’t work, as the figure/html doesn’t show up (but caps etc. are present)

Does anybody have an idea how to circumvent this?
I think it would be nice to have interactive figures/ a little bit of plotlys power in mne.report.

Thanks for your help!

Best
Rudi K

Hi Rudi,

I suspect that plotly has javascript code in the header that don’t work when you use report.add_htmls_to_section. Not sure if there is an easy way without understanding the architecture of the report.

FWIW, I tried using mpld3 for interactivity in the report long ago and it seemed to have worked easily.

So maybe you can try that route with add_htmls_to_section

Hope that helps,
Mainak

Yes, this is my guess too. I recently added a Report.add_custom_css method that allows one to add custom CSS to the HTML <head>; in the same way, we could add a method that would do a similar thing for JavaScript. You’d still have to manually extract the relevant parts of the plotly-generated HTML before inserting them into the report.

can you share a code snippet to start where you stopped?

Alex

@agramfort : are you asking @mainakjas or do you want me to provide a minimal working example?

If you can do it it’s great

Then I will see what I can do

Alex

no problem, here is a gist as a starter kit to look into this:

Thanks for your help!

Rudi

just do this:

report.add_htmls_to_section(fig.to_html(full_html=False), section=“test”, captions=“test_add_htmls”)

and it works

Alex

Hmm, so easy and yet too hard for me.
Thank you, Alex, it works perfectly :slight_smile:

Rudi