-
Notifications
You must be signed in to change notification settings - Fork 132
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
Add notebook directive #33
Conversation
4fd8fe8
to
8c2b8bd
Compare
Thanks for this PR! I hope it isn't actually your final PR. I'm looking forward to receiving more ... I'll have a closer look later, but first I want to comment on your suggested changes regarding The reason why I initially added However, all this became obsolete when I added the Now I had the choice to use either Anyway, I think that both are equally valid ways to use Does that sound reasonable? |
2a5cde7
to
aa681be
Compare
Yes, definitely reasonable! I agree that sticking with Do you think it's necessary to show an example of the notebook directive, or is adding a notebook as documentation sufficient? I think the most compelling example would be including a notebook that is not in the |
58e1154
to
212ea73
Compare
I think an example in the docs would be good, both as documentation and as test if it even works. I would add this to the end of doc/rst.rst and include a short notebook there. Either way, I think it would be useful to put the example notebook in a subdirectory, e.g. |
@@ -848,6 +889,7 @@ def setup(app): | |||
|
|||
app.add_directive('nbinput', NbInput) | |||
app.add_directive('nboutput', NbOutput) | |||
app.add_directive('notebook', Notebook) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'm not sure if it's good to call this notebook
, since that's already used by notebook_sphinxext.py
.
What about calling it nbinclude
to show the similarity with the built-in include
directive?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We previously used a modified version of notebook_sphinxext.py
, hence why I went with notebook
:) But nbinclude
makes a lot of sense and is a natural extension of the include
directive, and also the NbInput
and NbOutput
directives defined in nbsphinx, so I'll make that change!
60a833d
to
d1da027
Compare
I added an example in |
Thanks! I'll have a closer look at this in the next few days ... |
OK, I had a look ... I don't think it's necessary to make the detour over the reST string. Also, if #36 comes through, there won't be a reST string anymore. Do you still have the hash of your original commit? |
I found it: 212ea73 |
Yep, that's it :) The difference is that I was parsing directly into the document ( |
You shouldn't parse into I'm not sure if that's the best way, but it should work somewhat like this: node = docutils.utils.new_document('somename', self.state.document.settings)
nbparser.parse(rawtext, node)
return node.children |
Yes, I also tried that before settling on this current solution. The issue with making a new document, parsing into it, and returning the parsed doctree is that other parts of the document (specifically the Sphinx sidebar) gets included in That said, when I tried it returned |
I didn't test this extensively, but the sidebar looked OK ...
If |
I'll try it out! |
d1da027
to
5268883
Compare
One issue that I have now (that isn't related to the change to I added the orphan metadata to the |
This should be the explanation: http://www.sphinx-doc.org/en/stable/markup/misc.html#file-wide-metadata I don't know if that's the proper solution, but you can try this: if isinstance(node.children[0], docutils.nodes.field_list):
return node.children[1:]
return node.children ... or something along those lines ... This theoretically also removes |
@@ -36,6 +36,26 @@ These links were created with: | |||
* "``../``" is not allowed, you have to specify the full path even if the | |||
current source file is in a subdirectory! | |||
|
|||
Sphinx Directive for Including Notebooks |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I would prefer if you could move this section after the section about nbinput
and nboutput
.
raise self.severe(u'Problem with "%s" directive:\n%s' % | ||
(self.name, error)) | ||
|
||
# Use the NotebookParser to convert to reST and evaluate if needed |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Conversion to reST is an implementation detail and will likely go away with #36.
Can you please add links and images to the example notebook, similar to Somehow you'll have to manipulate the paths to get this working correctly. |
The added notebook is included in the `rst.rst` document, but not added to any toctrees.
This is a bit of a hack, but it means that notebooks no longer need to have the `orphan: true` metadata specified for any notebook that is in the doc directory and is included through the `nbinclude` directive, but is not in any toctrees.
5268883
to
6318526
Compare
Sorry for the delay! I think I've incorporated all the feedback now, and where it made sense, I made FIXUP commits so that you can see what I've changed compared to what was in this PR before. Before merging, I can fixup those commits in an interactive rebase to keep the history clean. I was able to remove the The only thing that I think is not really resolved thus far is adding links / images to the included notebook. The issue is that relative paths depend on the directory in which the notebook is executed, and there are two sets of relative paths to worry about: those written by the notebook creator, and those generated by
Whichever option we go with will need to be documented, in any case! |
Thanks for the changes! I think option 2 is the right choice. I'll have a closer look tomorrow ... |
OK, I had a closer look ... Most code assumes that the files are in the source directory, Links to notebooks outside of the source directory would also be quite hard to get working. The implementation would definitely become much more complicated and therefore harder to maintain in the future. Now let me take a step back and ask you this: Do you really need this? I see a few alternatives for your case:
I think I'd prefer alternative 2 in your case. All three of them would be hugely simpler than implementing (and probably even using) the What do you think? |
Unfortunately, option 2 is not a complete solution as this causes problems with images in output cells as detailed in #49. Also, this is problematic on Windows. Symbolic links have been supported for a while, but up to Windows 10 required elevated privileges to be created. Option 1 is in my case currently not possible (the notebooks need to be part of the installed Python package). |
@jgosmann Thanks for your comment. I know that each of the alternatives has its own weaknesses. I'm open to suggestions for solutions to the problems of this PR, probably we can solve them ... Another thing you could try is to use your You could also try https://github.com/ngoldbaum/RunNotebook, which I think lets you include notebooks from wherever, but AFAICT its integration with Sphinx is weaker (which may actually be what you want) and it has no LaTeX/PDF support. |
I've just stumbled upon the PyPI package |
Hadn't seen nbsphinx-include, thanks @mgeier, will check it out :) |
@tbekolay we used |
Yes, |
This is the fourth and final PR. In our project, the notebooks that we include in the documentation are examples of using our library. We have the examples in the top-level of the repository so that people visiting the repo see them front-and-center and can look through them. Unfortunately, since Sphinx doesn't allow
toctree
entries that are not in the root folder of the documentation, we can't include these examples simply by adding them totoctree
entries.Similar to how files like
README.rst
can be added with theinclude
directive, this PR adds anotebook
directive that you can point at a notebook outside of thedoc
directory. It also allows you to have one.rst
file that includes multiple notebooks, which I can imagine being useful.The actual implementation of the directive is basically how
docutils
'sinclude
directive works, but then uses theNotebookParser
to insert blocks into the document. 4fd8fe8 shows how this works by eliminating the duplication betweendoc/index.rst
anddoc/index.ipynb
. It seems like bi-directional communication works perfectly; now reST files can have notebooks in them, while notebooks can have raw reST cells in them! It's very nice, and definitely not something we were able to do with our previous implementation.One issue with the last commit is that when I build the Sphinx docs, I don't get links in the left sidebar, so I may be missing something critical in this PR. Similarly, the
subdir/toctree.ipynb
notebook doesn't show up, even though it is included inindex.ipynb
. Any idea why that might be?