MyST-NB icon indicating copy to clipboard operation
MyST-NB copied to clipboard

ENH: Ability to target mime types at the code-block level

Open mmcky opened this issue 3 years ago • 3 comments

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

It would be nice to be able to request a specific mime type for output at the code-block level for finer control in selecting which output to use. I am using sympy and a poor quality image is used in preference to LaTeX which would look nice in the output

The following code:

```{code-cell} python3
import sympy
from sympy import init_printing
# Creates algebraic symbols that can be used in an algebraic expression
g, r, x0 = sym.symbols('g, r, x0')
G = (1 + g)
R = (1 + r)
p0 = x0 / (1 - G * R**(-1))
init_printing(use_latex=True)
print('Our formula is:')
p0
```

is used to produce the following output (about 2/3 down the page)

Describe the solution you'd like

Add an option to https://jupyterbook.org/content/code-outputs.html#render-priority to enable specifying a specific mime type to use as the output (or perhaps a predefined order priority if that makes more sense). The example above outputs two mime types so that may need an order rather than a specific choice?

Describe alternatives you've considered

Change at the project level but that might not work for other code-blocks where image would work best

Additional context

Migrating this quantecon lecture which contains equations that are nicely formatted.

mmcky avatar Nov 03 '20 03:11 mmcky

Are you sure the code block level is optimal for this?

It appears that another alternative would be to control this on the IPython level by hooking into its display system (see here). That may be advantageous because you would probably want the same mime type priority for all objects of the same type.

That would require to run some initialization code at the beginning of each notebook, see also a related recent discussion https://github.com/executablebooks/meta/discussions/172

akhmerov avatar Nov 03 '20 14:11 akhmerov

its a good question @akhmerov -- and worth discussing more broadly -- It is how I have assumed I would fix a current issue by being able to specify a mime type for a specific case when using sympy but I have since solved the issue by switching on init_printing(use_latex="mathjax") and it appears the img isn't generated so LaTeX get's used.

An automatic / profile approach may be a better option for the majority of users for consistency as you suggest.

It's tricky in that you can usually address issues like this:

  1. selecting mime types when multiple representations are provided by a package (via priority ordering etc.).
  2. changing the code to output the mime type you want displayed in the end product

It is hard to strike the right balance.

mmcky avatar Nov 04 '20 02:11 mmcky

I think usually it's better to prioritize global configuration over local one since that requires less work from the user. Furthermore it's usually a good idea to not implement alternative approaches to what can already be achieved unless the situation is improved.

Both of these considerations seem to point against implementing per-cell mime control at this moment.

akhmerov avatar Nov 04 '20 13:11 akhmerov