-
Notifications
You must be signed in to change notification settings - Fork 174
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
Use nbsphinx to build examples for documentation #1349
Conversation
This was my original reason to investigate these types of options and make the PR I made for nbsphinx, but as time's passed I have to admit that I'm not as opposed to moving the notebooks to the
Hm, I've never experienced that behavior before... when I'm in my Python 2 environment, I get:
and in my Python 3 environment:
What do you get?
I'm sure that it can be adapted, but also I'm not sure how strongly I feel about these input cells being collapsed in the first place... perhaps we should remove the
Indeed, we should get rid of the progress bar in the docs... I have a few thoughts about how this might be done but would have to play around with a bit... |
In Nengo SPA I moved the examples folder into My
(and I can switch the virtualenv in a notebook by changing the kernel)
I'm probably ok with that.
Can you roughly describe your thoughts? |
Makes sense!
Hmm, do you not install
I'm not sure how Sphinx works exactly, but the first thing I'd try is manipulating |
My Jupyter kernels reside in the user location which is not related to the virtualenv, whereas yours are in the “environment” location. Whether jupyter is installed in the virtualenv affects only the process managing the kernels (usually I start it without an active virtualenv if I don't want to test something with a specific Jupyter version). I think we can't rely on the default kernel being the correct one. If we don't want the user to manually select the kernel, it might be possible to automatically install a kernel with the virtualenv's Python.
I think Sphinx launches independent Jupyter kernels which are unaffected by config manipulations (but I might be wrong).
I think this might actually work. Especially if we copy the examples, the file doesn't need to be removed. |
That works indeed, I added a commit. :) |
I think this should now be ready for review. I made In Nengo SPA I had some incompatiblitly with the numfig feature and numpydoc (nengo/nengo-spa@b61b770) and replaced numpydoc with
For those reasons it seems best to stick with numpydoc in core Nengo for now. |
One way to properly show the default values in the function signature would be to add docstrings like this one to the class Node(NengoObject):
# ....
def __init__(self, output=Default, size_in=Default, size_out=Default,
label=Default, seed=Default):
"""__init__(output=None, size_in=None, size_out=None, label=None, seed=None)"""
# ... But I'm not sure if we want to do that:
|
We discussed this at the dev meeting today and were generally in favor of moving the examples to the
To deal with these issues, we will 1) either manually add some text in the examples folder to say where all the examples live in the repo, and 2) make sure that the docs still include a link to download the |
So, just adding a
As in, adding this as a header to the rendered/uploaded docs? Also, why not do both of these things? |
No, the text would have to be in the rendered docs, as in the ideal case users would never need to know about the organization of the source tree and mostly interact with Nengo by pip installing and looking through the docs.
Ideally this would be done automatically in the conversion from notebook to doc page. With nbsphinx, the only way to add this as a header would be to include it in the notebook itself, which feels weird. |
Given this new information, I vote for the second option of adding a header to the rendered page, which would understandably require a modification to |
I added a commit that changes the “View page source” link at the top right of normal documetation pages to a “Download example as Jupyter notebook” link for the examples. This is achieved by two things:
Let me know what you think of these changes. |
387924a
to
68fe598
Compare
Rebased, apparently the documentation theme has changed. With the guzzle theme the template modification actually requires less boilerplate, so that is nice. :) I think the only remaining thing is to add some documentation pointing to the new location of the examples folder. However, I am not sure where that should go and what it should say, @tbekolay. To me it doesn't seem particularly obscure to have the examples in the docs folder and I don't think we have any other parts of the folder structure documented (which doesn't mean that it isn't a good idea). |
I mentioned that it would be nice to automatically extract parameter defaults from the source code and I think it might be possible with a small Sphinx extension, but that is probably something for a separate PR. |
Note: TravisCI is failing for reasons unrelated to this PR. |
OK I've rebased this now and fixed issues from the rebase; if there are no objections to the current state of this PR I'll compress the history a bit and merge after lunch. |
b689662
to
45b090a
Compare
In order to do this, examples were moved to the docs folder.
When including them in the docs with nbsphinx, headings are real reST headings, meaning they can be included in toctrees. For that reason, we: - Ensure there is only one level 1 heading - Ensure headings have sentence case (not capitalized) - Add metadata for syntax highlighting
Obsolete as it did the same job as nbshpinx.
There was code for selecting the appropriate kernel when building the notebooks with nbsphinx in the PR (see discussion above), but that seems to have completely vanished? 😮 I still think it should be included, though I don't care as much about Nengo core as I usually don't have to build the documentation. But the same code seems to have gone missing from Nengo SPA too (or was it never in there?) which right now prevents me from building the docs with the correct kernel/virtualenv. 😞 |
Luckily I found a local copy of the code. |
Yeah, I removed that from this core PR, but I didn't touch anything in Nengo SPA. |
As for why I removed it, it just seemed out of place in |
But it cannot be handled without modifying |
You can pass that in to |
Does that somehow work with |
I'm not sure, but it's definitely easier to update the documentation than have that in |
Makes sense. I rescued the code into a Gist in case I or someone else wants to refer to it at a later point. |
Motivation and context:
As noted in #1308 it is not possible to build the examples for inclusion in the documentation with current versions of Jupyter. While the solution so far was to install older versions, this should eventually be fixed and as I have the same issue with the documention of Nengo SPA, I decided to spend some time on this.
@tbekolay pointed me towards nbsphinx which allows to include Jupyter notebooks into Sphinx documentation with good integration with the Sphinx theme. However, Sphinx does not allow to have source files outside of the documentation folder (for security reasons I believe), but our examples folder lives outside the documentation folder. I am opposed to changing that, and I think @tbekolay agrees. So other potential solutions are:
conf.py
. This is the solution I chose in this branch because it was pretty much the path of least resistance. It seems like sphinx manages, despite the copied notebooks being overwritten in each run, to just re-excute notebooks that have been changed.Another issue (which might also exist in the current system, but I haven't verifiied it): The Jupyter notebooks are executed by a Jupyter kernel that is independent from whatever virtualenv is active. It seems like a bad idea to just use the default kernel that might have the wrong version of Nengo installed (in particular true for my environment). Thus I added some code to conf.py to ask for which kernel to use (and the possibility to fix it via an environment variable).
This PR is still work-in-progress (see to do list below), but at this stage it would be good to get an idea if the general direction seems good.
Interactions with other PRs:
ctn-waterloo/devscripts#6 changes the clearoutput script to add metadata to the notebooks that is required to make syntax highlighting with nbsphinx work. I ran the script on all existing examples to add that metadata.
How has this been tested?
build the docs and looked at through them
How long should this take to review?
Types of changes:
Checklist:
Still to do:
examples/REAME.rst
,examples/usage/exceptions.ipynb
,examples/usage/strings.ipynb
). Figure out whether these should indeed be skipped in the documentation (and silence these warnings) or include them in documenation in an appropriate way.hide_input
function to work in Jupyter notebook and the Sphinx output (at least the Javascript gets included).