jupyter / jupyter-sphinx

Sphinx extension for rendering of Jupyter interactive widgets.
https://jupyter-sphinx.readthedocs.io/
BSD 3-Clause "New" or "Revised" License
187 stars 65 forks source link

Allow adding raw inputs and outputs #141

Closed willsimmons1465 closed 3 years ago

willsimmons1465 commented 4 years ago

Proposed solution for issue #140

Adds an extra option dont-run to cells which means they are not submitted for execution, just simply rendered as a cell. Current drawback of this solution is that the cell is not included in the .ipynb or .py output files. Adds two new directives, jupyter-input and jupyter-output which build a CellInputNode or CellOutputNode directly. These are added to the .ipynb and .py output files when built. Comments and suggestions welcome.

akhmerov commented 4 years ago

Hm, unfortunately the fix is not as simple, check out the resulting docs: https://86-73843754-gh.circle-artifacts.com/0/html/index.html

The problem is that in order for the styles to be correctly applied, everything should be enclosed in a jupyter cell container.

So in addition to that, we need to make a JupyterCell directive, which would create a marked JupyterCellNode that has its execution skipped. Optimally, we'd also need to attach the input part of that node in the correct place of the notebook, but only after execution, so that the code isn't run. This, however is optional.

Additionally I suggest to rename the docs section to something like "Advanced usage: manually forming Jupyter cells"

willsimmons1465 commented 4 years ago

@akhmerov Found it easier to just start from master again. Got the formatting mostly there, but can't get the provided inputs and outputs to appear in the same JupyterCellNode. If that's what you are talking about, then I think I might have to design it with nested directives, e.g. having jupyter-input and jupyter-output within a jupyter-cell directive

akhmerov commented 3 years ago

Sorry, took me a while to come back to this.

I have looked at the implementation and all looks good except for two related aspects:

image

willsimmons1465 commented 3 years ago
  • Probably related: the outputs and inputs seem to be contained within different jupyter cell containers, which results in a different rendering (a gap between the input and the output). Let's instead detect when they are neighbors in the doctree and combine them into a single one. Let me know if you would benefit from advice on the implementation.

I have added an extra pre-processing transform to combine the JupyterCellNodes from input and outputs when adjacent. The formatting looks much more consistent now which is nice

akhmerov commented 3 years ago

Yes, this is great!

Looking at the tests I see that we assume that output without input is an error, and don't assume that input without output is an error. This is appropriate, however let's make it official and add a test that only jupyter-input does work and produces a cell without output.

After that I think all good to go!

willsimmons1465 commented 3 years ago

Because a CellOutputBundleNode is always added to each jupyter cell (see this line), we will still have an output cell when only jupyter-input is given, but we can check that it is empty and hidden

akhmerov commented 3 years ago

Awesome, thanks for your work @willsimmons1465!