Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow Bokeh plots to be glued and pasted #335

Closed

Conversation

bryanwweber
Copy link
Contributor

Using the JSON-type output from Bokeh, this creates appropriate HTML and
Javascript nodes in the output when the Bokeh+JSON mimetype is found.
The Python Bokeh library is only imported when it is needed, to avoid a
package dependency on Bokeh. Users will need to install Bokeh via pip or
conda to be able to glue/paste Bokeh plots. Using the JSON output allows
plots to be pasted into figures as well as a general element with no
further changes.

Uses the Sphinx configuration to decide whether a page has Bokeh content
and only loads BokehJS on those pages. This simplifies the script that
needs to be embedded in each paste and ensures that Bokeh is only loaded
once from the CDN, unless the user runs output_notebook(). In the case
where the user runs output_notebook() the BokehJS library is loaded
twice but this doesn't appear to cause any problems.

Using the JSON-type output from Bokeh, this creates appropriate HTML and
Javascript nodes in the output when the Bokeh+JSON mimetype is found.
The Python Bokeh library is only imported when it is needed, to avoid a
package dependency on Bokeh. Users will need to install Bokeh via pip or
conda to be able to glue/paste Bokeh plots. Using the JSON output allows
plots to be pasted into figures as well as a general element with no
further changes.

Uses the Sphinx configuration to decide whether a page has Bokeh content
and only loads BokehJS on those pages. This simplifies the script that
needs to be embedded in each paste and ensures that Bokeh is only loaded
once from the CDN, unless the user runs output_notebook(). In the case
where the user runs output_notebook() the BokehJS library is loaded
twice but this doesn't appear to cause any problems.
@damianavila
Copy link

@choldgraf @chrisjsewell, I am interested to know your thoughts about adding pieces like this one that interfaces with 3rd party libraries. There is some preliminary conversation on the following previous discussion (in case you need some context): executablebooks/meta#346.

@chrisjsewell
Copy link
Member

chrisjsewell commented Jun 16, 2021

Hey @bryanwweber @damianavila 👋🏼 Essentially I feel these third party interfaces to myst-nb (and thus jupyter-book) should probably be in a separate plugin package, and loaded via entry points

this is already possible via load_renderer And the myst_nb.mime_render entry point, although happy to discuss if this interface could be improved

@bryanwweber
Copy link
Contributor Author

Thanks @chrisjsewell Any pointers to how to do that in the docs or elsewhere?

@choldgraf
Copy link
Member

@bryanwweber there are some docs here:

https://myst-nb.readthedocs.io/en/latest/use/formatting_outputs.html#customise-the-render-process

but this one might be a test-case that drives some enhancements to the documentation (e.g., maybe a short tutorial on implementing your own renderer) I am happy to help write out some steps on how to do this, I think this would be a great way to extend MyST-NB's functionality without adding a lot of complexity to the core (e.g., myst-nb-bokeh could be a dependency)

For reference, here's load_renderer function:

def load_renderer(name: str) -> "CellOutputRendererBase":

and here's where the entry points are defined:

myst_nb.mime_render =

@bryanwweber
Copy link
Contributor Author

@choldgraf @chrisjsewell Thanks for the references, I'll look into it! I certainly appreciate the desire to keep the core of MyST-NB small! 😄

It seems like this plugin would solve the rendering issue, but not the glueing issue. Using the current myst_nb.glue on a Bokeh Figure stores the HTML for a Bokeh table, plus a plain-text string in the output of the cell.

I think glueing a Bokeh figure would then require a separate function (e.g., glue_bokeh()) defined in the plugin package. To me, it seems like a single UX/API for gluing would be better - why should a user care whether they're gluing a Bokeh figure or Matplolib figure or Python object or anything else?

Do you have any thoughts/opinions about that idea? Thanks!

@bryanwweber
Copy link
Contributor Author

@choldgraf @chrisjsewell OK, so I tried this approach of loading a renderer from an entry point. Unfortunately, this does not allow pasting either, because the renderer is always set to "default":

output_node = CellOutputBundleNode([output], "default")

If I change that string to point to my new renderer entry point, then it works 🎉 So this is going to definitely require changes to myst_nb beyond just potentially importing another extension. I assume you'd like me to make any required changes in a new branch/new PR?

@bryanwweber
Copy link
Contributor Author

@choldgraf @chrisjsewell I'm going to close this. I created an extension package over here: https://github.com/bryanwweber/MyST-NB-Bokeh I'll do some more cleanup and add testing in the next little while. I'll make a separate PR for the fix to MyST-NB that's needed to get it working properly, as I mentioned above ☝️

FWIW, the documentation was not super helpful in creating this. I'd be happy to write up a blog post/bit of documentation about how I got all this working. Let me know what's the best place for that. Thanks for the pointers!

@chrisjsewell
Copy link
Member

Heya yeh no worries cheers

@damianavila
Copy link

FWIW, the documentation was not super helpful in creating this. I'd be happy to write up a blog post/bit of documentation about how I got all this working. Let me know what's the best place for that. Thanks for the pointers!

@choldgraf, what would be a good place for this? I do not want to lose the generated momentum created by @bryanwweber 😉

@damianavila
Copy link

I'll make a separate PR for the fix to MyST-NB that's needed to get it working properly, as I mentioned above

Btw, that PR lives here (crosslinking for easier ref): #337

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

4 participants