##// END OF EJS Templates
Merge pull request #13656 from meeseeksmachine/auto-backport-of-pr-13652-on-7.x...
Blazej Michalik -
r27636:a812063a merge
parent child Browse files
Show More
@@ -1,60 +1,60 b''
1
1
2 .. _shell_mimerenderer:
2 .. _shell_mimerenderer:
3
3
4
4
5 Mime Renderer Extensions
5 Mime Renderer Extensions
6 ========================
6 ========================
7
7
8 Like it's cousins, Jupyter Notebooks and JupyterLab, Terminal IPython can be
8 Like it's cousins, Jupyter Notebooks and JupyterLab, Terminal IPython can be
9 thought to render a number of mimetypes in the shell. This can be used to either
9 thought to render a number of mimetypes in the shell. This can be used to either
10 display inline images if your terminal emulator supports it; or open some
10 display inline images if your terminal emulator supports it; or open some
11 display results with external file viewers.
11 display results with external file viewers.
12
12
13 Registering new mimetype handlers can so far only be done my extensions and
13 Registering new mimetype handlers can so far only be done by extensions and
14 requires 4 steps:
14 requires 4 steps:
15
15
16 - Define a callable that takes 2 parameters:``data`` and ``metadata``; return
16 - Define a callable that takes 2 parameters:``data`` and ``metadata``; return
17 value of the callable is so far ignored. This callable is responsible for
17 value of the callable is so far ignored. This callable is responsible for
18 "displaying" the given mimetype. Which can be sending the right escape
18 "displaying" the given mimetype. Which can be sending the right escape
19 sequences and bytes to the current terminal; or open an external program. -
19 sequences and bytes to the current terminal; or open an external program. -
20 - Appending the right mimetype to ``ipython.display_formatter.active_types``
20 - Appending the right mimetype to ``ipython.display_formatter.active_types``
21 for IPython to know it should not ignore those mimetypes.
21 for IPython to know it should not ignore those mimetypes.
22 - Enabling the given mimetype: ``ipython.display_formatter.formatters[mime].enabled = True``
22 - Enabling the given mimetype: ``ipython.display_formatter.formatters[mime].enabled = True``
23 - Registering above callable with mimetype handler:
23 - Registering above callable with mimetype handler:
24 ``ipython.mime_renderers[mime] = handler``
24 ``ipython.mime_renderers[mime] = handler``
25
25
26
26
27 Here is a complete IPython extension to display images inline and convert math
27 Here is a complete IPython extension to display images inline and convert math
28 to png, before displaying it inline for iterm2 on macOS ::
28 to png, before displaying it inline for iterm2 on macOS ::
29
29
30
30
31 from base64 import encodebytes
31 from base64 import encodebytes
32 from IPython.lib.latextools import latex_to_png
32 from IPython.lib.latextools import latex_to_png
33
33
34
34
35 def mathcat(data, meta):
35 def mathcat(data, meta):
36 png = latex_to_png(f'$${data}$$'.replace('\displaystyle', '').replace('$$$', '$$'))
36 png = latex_to_png(f'$${data}$$'.replace('\displaystyle', '').replace('$$$', '$$'))
37 imcat(png, meta)
37 imcat(png, meta)
38
38
39 IMAGE_CODE = '\033]1337;File=name=name;inline=true;:{}\a'
39 IMAGE_CODE = '\033]1337;File=name=name;inline=true;:{}\a'
40
40
41 def imcat(image_data, metadata):
41 def imcat(image_data, metadata):
42 try:
42 try:
43 print(IMAGE_CODE.format(encodebytes(image_data).decode()))
43 print(IMAGE_CODE.format(encodebytes(image_data).decode()))
44 # bug workaround
44 # bug workaround
45 except:
45 except:
46 print(IMAGE_CODE.format(image_data))
46 print(IMAGE_CODE.format(image_data))
47
47
48 def register_mimerenderer(ipython, mime, handler):
48 def register_mimerenderer(ipython, mime, handler):
49 ipython.display_formatter.active_types.append(mime)
49 ipython.display_formatter.active_types.append(mime)
50 ipython.display_formatter.formatters[mime].enabled = True
50 ipython.display_formatter.formatters[mime].enabled = True
51 ipython.mime_renderers[mime] = handler
51 ipython.mime_renderers[mime] = handler
52
52
53 def load_ipython_extension(ipython):
53 def load_ipython_extension(ipython):
54 register_mimerenderer(ipython, 'image/png', imcat)
54 register_mimerenderer(ipython, 'image/png', imcat)
55 register_mimerenderer(ipython, 'image/jpeg', imcat)
55 register_mimerenderer(ipython, 'image/jpeg', imcat)
56 register_mimerenderer(ipython, 'text/latex', mathcat)
56 register_mimerenderer(ipython, 'text/latex', mathcat)
57
57
58 This example only work for iterm2 on macOS and skip error handling for brevity.
58 This example only work for iterm2 on macOS and skip error handling for brevity.
59 One could also invoke an external viewer with ``subprocess.run()`` and a
59 One could also invoke an external viewer with ``subprocess.run()`` and a
60 temporary file, which is left as an exercise.
60 temporary file, which is left as an exercise.
General Comments 0
You need to be logged in to leave comments. Login now