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

Referencing output from cells #851

Open
romanlutz opened this issue Jul 24, 2021 · 3 comments · May be fixed by #852
Open

Referencing output from cells #851

romanlutz opened this issue Jul 24, 2021 · 3 comments · May be fixed by #852

Comments

@romanlutz
Copy link

romanlutz commented Jul 24, 2021

I can reference code from a notebook using

.. literalinclude:: ../auto_examples/plot_myscript.py
    :start-after: [some-tag]
    :end-before: [another-tag]

and the resulting images using

.. figure:: ../auto_examples/images/sphx_glr_plot_myscript_001.png
    :target: ../auto_examples/plot_myscript.html

However, it doesn't seem to be possible to reference output from code that's part of a sphinx-gallery. This would be very useful.

I did find the following in the generated .rst file:

.. rst-class:: sphx-glr-script-out

 Out:

 .. code-block:: none

    <the output>

Perhaps if we had a dedicated output file (which we do for images) it would be possible? Thanks for considering!


Update: I found a way to hack this together in the meanwhile, but I don't particularly like it. One of the output files is the .rst file which includes printed output. I added the following code to my plotting python script

output_marker = "OUTPUT_"
print(f"[{output_marker}_START]")
<code with interesting output that I want to show elsewhere goes here>
print(f"[{output_marker}_END]")

[Amazingly, the separation of the string is necessary, otherwise literalinclude below will show the code, not the output!]

... and then simply add the following where you show the code:

.. literalinclude:: auto_examples/plot_myscript.rst
   :start-after: [OUTPUT_START]
   :end-before: [OUTPUT_END]

Reasons why I don't like this:

  • The example code gets polluted with the output marker code.
  • The output could change from build to build without me noticing. Doctest solves this nicely, but if I want to use the same code snippet in several place I have to duplicate it every time. That's why I thought of creating an example and using literalinclude instead in the first place.
@larsoner
Copy link
Contributor

If literalinclude can work off of comments in RST, it would be trivial to add:

.. rst-class:: sphx-glr-script-out

 Out:

.. sphx-glr-output-block-1-start
 .. code-block:: none

    <the output>

.. sphx-glr-output-block-1-end

...

Out:

.. sphx-glr-output-block-2-start
...

@romanlutz do you want to give it a shot and see if it works? I think it would require modifying the CODE_OUTPUT variable in gen_rst.py

@romanlutz
Copy link
Author

I tried this and it almost works, except that the literalinclude output starts with

.. code-block:: none

<the output>

We couldn't put it after the code-block starts, could we?

@romanlutz
Copy link
Author

Okay, nevermind, one can do literalinclude starting from line 2

Out:
.. literalinclude:: ../auto_examples/plot_myscript.rst
    :start-after: .. sphx-glr-output-block-008-start
    :end-before: .. sphx-glr-output-block-008-end
    :lines: 2-

The numbering of blocks doesn't start at 001 right now, though, but are rather numbered based on the index of the code block. I'll share the PR shortly and will be looking forward to feedback.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants