Skip to content

Support for code-cell to read code from a file #274

Description

@mmcky

Is your feature request related to a problem? Please describe.

Within a large lecture series it can be a pretty common pattern to re-use code in different documents. In the past we have used literalinclude to bring in code from a file. As sphinx parses code eventually in the literal_block method it works pretty well.

The issue with myst_nb/jupyter-book is this no longer works when building in this context as the literalinclude is not brought in until sphinx is called. This means the code won't get executed at the jupyter_cache level as the literalinclude is contained in the markdown block when converting using jupytext. The code will be visible in the end HTML but not executed prior to building with sphinx.

Describe the solution you'd like

A solution to this would be to add :file: option to code-cell directives for including code that is meant to be executed.

```{code-cell} python3
:file: _code/code.py
```

Typically file type attributes are usually arguments in sphinx but I think it would be better in this case to retain the simplicity of optional language argument.

The more typical sphinx approach would be

```{code-cell} _code/code.py
```

Describe alternatives you've considered

None -- changing the behaviour of literalinclude to be parsed as code is not a viable solution given out jupytext works.

Additional context

This is currently an issue for migrating the python.quantecon.org series as literalincludes are used pretty extensively in 6 lectures. We could copy the code across these lectures but would prefer not to do that to assist with maintaining the code and consistency across lectures that use the same code.

Metadata

Metadata

Assignees

No one assigned

    Labels

    enhancementNew feature or request

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions