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

Implement NbGlueDomain #73

Merged
merged 32 commits into from
Mar 15, 2020
Merged

Implement NbGlueDomain #73

merged 32 commits into from
Mar 15, 2020

Conversation

chrisjsewell
Copy link
Member

@chrisjsewell chrisjsewell commented Mar 14, 2020

closes #70 and also relates to #72

An initial draft of reworking the paste roles/directive into a domain. The domain achieve's two things:

  1. It collates all roles/directives relating to rendering of 'glue' objects. So all you need to have in the extension setup is app.add_domain(NbGlueDomain).
  2. It centralises access to the glue cache. This has allowed me, for example, to also track the notebook from where each glue key comes from, and remove keys related to a deleted document (see NbGlueDomain.clear_doc)

I have then added the PasteFigure directive as an example of extending the functionality of the domain for handling different output types. It allows for captions, labelling/referencing, width and alignment.
Take a look at:
https://173-241268229-gh.circle-artifacts.com/0/html/use/glue.html#using-the-nb-figure-directive
😄

Here we create a specific `PasteTextNode`, and
move most of the logic for formatting etc out of the `PasteNodesToDocutils` transform.

This makes the transform more generic, so that we can add more PasteNode subclasses to it in the future.

We also rename the role from `nb:paste` to `nb:text`,
to reflect its specific nature,
and rewrite the documentation to reflect this.
@chrisjsewell
Copy link
Member Author

chrisjsewell commented Mar 14, 2020

In d4aa616, I have refactored the paste role and rewritten the documentation accordingly:
https://178-241268229-gh.circle-artifacts.com/0/html/use/glue.html#pasting-glued-variables-into-your-page

@choldgraf
Copy link
Member

this looks awesome - I'll take a look soon!

@chrisjsewell
Copy link
Member Author

I was also thinking of changing nb:paste to nb:output. I'm not fixed on the naming though and am open to other suggestions.

@choldgraf
Copy link
Member

took a look at the docs - the figure bit is super cool!!

A few quick thoughts:

  • I wonder if the nb: prefix will be clear to people. It seems reasonable to me, but we should shop that around with the team to see if it is intuitive for them. Other options I can think about just for brainstorming purposes:
    • glu:
    • myst:
  • I kind like paste over output because I think it is more specific. calling it nb:output makes me assume it will be the output of a cell (which, one day, we may want to explicitly for cell outputs?).
  • For the nb:text role, could this be nb:paste as well, with the assumption that by default, anything pasted in-line will be pasted as text?
    • To give myself a counter-point. I suppose (one day) we could try to add in-line options for things like in-line images (e.g., for sparklines) or for in-text HTML (like tangle.js)

@chrisjsewell
Copy link
Member Author

To give myself a counter-point. I suppose (one day) we could try to add in-line options for things like in-line images

Yeh that was my thinking, that this role (with the formatting aspect) is very specific to text

@choldgraf
Copy link
Member

choldgraf commented Mar 14, 2020

to further prove my counter point, I tried adding ![](logo.png) in-line to some parts of the docs, and this is what it looked like:

image

I think this means we can basically already include "sparkline"-style images easily. That is super cool. All we'd need to do is insert that in the doctree and add a css rule like img.glue_inlue {max-height: 1em}

@chrisjsewell
Copy link
Member Author

Yeh maybe glu is more descriptive: glu:text, glu:figure. In this context glu:paste doesn't really make sense, so maybe glu:object?

@choldgraf
Copy link
Member

@chrisjsewell yeah I was thinking that if we used glu: then we wouldn't need paste

@chrisjsewell
Copy link
Member Author

chrisjsewell commented Mar 14, 2020

@chrisjsewell yeah I was thinking that if we used glu: then we wouldn't need paste

nb:paste -> glu:? (glu:object, glu:block, glu:any ?)

@choldgraf
Copy link
Member

choldgraf commented Mar 14, 2020

yeah - I was thinking then we'd always follow a glu:objecttype pattern.

Or maybe pst: for "paste"? We could register both pst: and paste: domains?

Edit: I wonder if domains can also be used as a role/directive as well? E.g. could glu: or pst: be used as a kind of glu:any drop-in?

@chrisjsewell
Copy link
Member Author

We could register both pst and paste domains?

No I think only one domain name is allowed

@chrisjsewell
Copy link
Member Author

chrisjsewell commented Mar 14, 2020

Ok, for now, in 19b2b0e I have change the domain name to glu and changed nb:paste to glu:any.
I like any because it is reminiscent of the sphinx any reference role.

I have also added a glu:any (inline) role. This has required that I hack together an 'inline' version of cell_output_to_nodes:
https://github.com/ExecutableBookProject/MyST-NB/blob/19b2b0edf0b4c686f4d21eb9a6d40c1b3e119f7f/myst_nb/transform.py#L64

Obviously this should be up-streamed to jupyter-sphinx.

See https://179-241268229-gh.circle-artifacts.com/0/html/use/glue.html#pasting-glued-variables-into-your-page 😄

def create_node(self, output: dict):
"""Create the output node, give the cell output."""
# the whole output chunk is deposited and rendered later
# TODO move these nodes to separate module, to avoid cyclic imports
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I hope that we can just import these nodes from jupyter-sphinx at some point, whenever I can figure out the tests in jupyter/jupyter-sphinx#107

@@ -49,3 +60,123 @@ def apply(self):
continue
col = ImageCollector()
col.process_doc(self.app, node)


def cell_output_to_nodes_inline(cell, data_priority, write_stderr, dir, thebe_config):
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This one you'd like to upstream yeah? What's the main difference w/ jupyter-sphinx one?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yep. All the nodes returned should be inline versions, i.e. literal_block -> literal, container -> inline, math_block -> math

@choldgraf
Copy link
Member

This is looking really good to me - I like the API decisions in here, and we can always re-visit after others have had a chance to play with them.

@choldgraf
Copy link
Member

(FYI this is still in "draft" state but I am +1 to merge when you are happy with it)

So that e.g. tables inherit the same CSS
@chrisjsewell
Copy link
Member Author

chrisjsewell commented Mar 15, 2020

(FYI this is still in "draft" state but I am +1 to merge when you are happy with it)

Two last changes for you to review above, both were made so that pandas tables are formatted properly in figures here: https://189-241268229-gh.circle-artifacts.com/0/html/use/glue.html#tbl-df

@chrisjsewell
Copy link
Member Author

If you do want to then merge, please add this as the commit message 😁

This commit introduces the NbGlueDomain for controlling glue outputs.
The domain achieves two things:

  1. It collates all roles/directives relating to rendering of 'glue' objects. So all you need to have in the extension setup is app.add_domain(NbGlueDomain).
  2. It centralises access to the glue cache, ensuring that keys related to deleted documents are removed

Multiple glue roles/directives have been introduced for formatting the output, including the PasteFigure directive, which allows for captions, labelling/referencing, width and alignment.

@chrisjsewell chrisjsewell marked this pull request as ready for review March 15, 2020 01:53
@chrisjsewell
Copy link
Member Author

@choldgraf
Copy link
Member

This looks great to me - let's give it a merge and see how things feel as folks on the team use this, I am excited!

@choldgraf choldgraf merged commit 4d83e8c into master Mar 15, 2020
@choldgraf choldgraf deleted the paste-domain branch March 15, 2020 05:36
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.

Consider using a sphinx domain for paste roles/directives
2 participants