✨ NEW: Add file load option for code-cells#286
Conversation
Codecov Report
@@ Coverage Diff @@
## master #286 +/- ##
==========================================
- Coverage 87.53% 87.48% -0.06%
==========================================
Files 12 12
Lines 1316 1334 +18
==========================================
+ Hits 1152 1167 +15
- Misses 164 167 +3
Flags with carried forward coverage won't be shown. Click here to find out more.
Continue to review full report at Codecov.
|
|
Very useful and nice simple syntax. |
|
I think this is a pretty straightforward solution on the myst nb side. I suspect that the main downside is that we are breaking from the “two way lossless conversion to ipynb via jupytext only” constraint that we have been following thus far. This is similar to the “code cells can’t be nested in other things” question as well, I think. I guess to me the main question is how to gracefully support this “extra” functionality in a way that doesn’t confuse people, or create strong maintenance burden, etc. |
I agree, what I propose is the following:
|
this may not be overly broken but will need to dig a bit further. They way I have chosen to update |
|
@mmcky that's a good point - the resulting ipynb will be two-way lossless, it's just that the ipynb created by jupytext alone (w/o myst nb) will have an empty code cell with the "file" metadata in it. Is that right? |
@choldgraf I think that is what would happen -- but I will confirm this in the morning :-). |
|
@choldgraf So I had a quick look -- I used the following source ---
jupytext:
text_representation:
extension: .md
format_name: myst
kernelspec:
display_name: Python 3
language: python
name: python3
---
# Simple Example
```{code-cell} python3
:file: _static/test.py
```which builds the {
"cells": [
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# Simple Example"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"file": "_static/test.py"
},
"outputs": [],
"source": []
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 3",
"language": "python",
"name": "python3"
}
},
"nbformat": 4,
"nbformat_minor": 4
}listing the The return to ```{code-cell}
:file: _static/test.py
``` |
|
@chrisjsewell within the test framework do you know how I can copy a support file to the |
|
@mmcky I reckon we can copy the support file explicitly from the source to the "temp srcdir" . Implicit copying does not sound feasible/practical to me. |
|
ok thanks @AakashGfude I will try and add that in today and see if that works. |
|
@AakashGfude is 48129ad what you had in mind? |
that's right @mmcky. Now, you just need to push |
|
thanks @AakashGfude with your help in getting this test infrastructure updated. |
|
Thanks for helping with this @choldgraf and @chrisjsewell . Switching over the main section of our lectures has been stalled on this feature for quite a while and we're a bit stuck right now. Once it's available we'll be able to push forward with shifting over the rest of the QE material, which is a sizable chunk of what was promised in the grant proposal. |
Notebooks should be able to be executed before "entering" sphinx; this was part of the modular design we originally conceived of, i.e. you should be able to use jupyter-cache independently to execute the notebooks and cache them, then run the sphinx build after and it just pulls from this cache. |
There was a problem hiding this comment.
In 8621c99 I improved the code, which highlighted an issue that needs fixing: docname is not available when using jupyter-cache.
If you fix this so that the tests pass, then I will approve
@chrisjsewell is
|
yep, see https://www.sphinx-doc.org/en/master/extdev/appapi.html?highlight=config-inited#sphinx-core-events, I believe docname is only injected in the loop after step 4, execution is handled in step 3 |
yeh but this uses the |
Oh I see. I put 43cc5cc together assuming |
|
ok I've basically rewritten the whole thing now lol:
|
|
@AakashGfude I see you are still active, if this looks ok I will merge? |
@chrisjsewell The code looks ok from my side.👌 |
|
Thanks everybody for all the hard work in thinking this one through and in implementation! |
|
hey @chrisjsewell thanks for the changes. They look good. |
|
Hi all, nice changes! I'd like to understand the implications of the change to the design principles better. Previously, I thought, that the result of converting myst to ipynb with jupytext, then executing directly, and then converting back should yield the result of what myst-nb does. Is this still the case? If not, what's the design constraint then? |
|
hi @akhmerov I have chosen the current syntax (in part) as it will currently roundtrip (myst:md -> ipynb -> myst:md) through There has been some discussion around possibly moving this downstream to Note: We need this to move the |
|
@mmcky it might be helpful if you wrote up the quantecon use case in a discussion or something, so that others know the reason that you need to load one python file across multiple notebooks. Is this for import statements or something? |
|
Sure @choldgraf it is mainly for sharing functions across lectures. You can develop some code in an earlier lecture and then re-use the function or class later (while still displaying the Once |
|
This is wonderful. I agree it is useful to be able to replicate code across parts. Does it support importing only part of the file; e.g. a start and end line number? I looked at the code quickly, but it didn't seem so. |
|
@mmcky thanks for sharing! I think it'd also be helpful to hear why this isn't resolvable by using |
|
I would say it is resolvable in that way but with more effort. If the code is a bunch of small examples that you don't really want to maintain a package for, and which are frequently edited, then the solution proposed here is much easier to manage. I suppose the other option is to inject the code into the md files using a preprocessing script before compiling. I'm not keen on that approach for our source material because our goal is to use plain vanilla JB and its extensions, rather than maintaining extra scripts. |
|
@jstac just to clarify, I am not arguing against it, I'm just saying that it would be helpful to have a writeup somewhere that clarifies these things, so that we can point to it in discussions, design decisions, etc! |
|
sure thing @choldgraf I will document a use case and design decisions etc. Probably for https://executablebooks.org/en/latest/ if people don't want to distribute this more widely at the moment. I will then link to it from agree with all of @jstac comments. In addition a package is not a good way to show source code -- but is useful for routines that are commonly used for execution. For those items we use the open source QuantEcon.py package |
|
@mmcky you could also just make it a "discussion" in the executablebooks/meta discussions board 👍 |
This PR enables the use of a file option for
code-cellblocks such as:Discussion:
This is a pretty simple way forward to solve #274 at the
myst_nblevel during the conversion frommdtoipynbfile. It essentially looks for afileoption attribute and if one is found it brings the contents of the file in and replaces the body_lines from the parsedcode-blocktoken. This produces anipynbthat has the contents of the file as a code-block.The downsides of implementing this feature here are:
myst_nbprovides this feature (rather than coremyst-parser) so wouldn't be considered part ofmyst.mdfile sources.It is my sense that this feature would rarely be used at the
ipynblayer so this could be considered a nice convenience feature ofmyst_nbbut hesitate to cause confusion with other parser features. This small addition would solve our issue of bringingexecutable code in from a file-- but understand if others think this might be better implemented elsewhere. I am open to thoughts and discussion.Note: this is not complete as it would needtests,docs, and somerobustnessadditions