file_formats.rst 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271
  1. .. _file-and-output-formats:
  2. File and Output Formats
  3. -----------------------
  4. JupyterLab provides a unified architecture for viewing and editing data
  5. in a wide variety of formats. This model applies whether the data is in
  6. a file or is provided by a kernel as rich cell output in a notebook or
  7. code console.
  8. For files, the data format is detected by the extension of the file (or
  9. the whole filename if there is no extension). A single file extension
  10. may have multiple editors or viewers registered. For example, a Markdown
  11. file (``.md``) can be edited in the file editor or rendered and
  12. displayed as HTML. You can open different editors and viewers for a file
  13. by right-clicking on the filename in the file browser and using the
  14. “Open With” submenu:
  15. .. image:: images/file_formats_open_with.png
  16. :align: center
  17. :class: jp-screenshot
  18. To use these different data formats as output in a notebook or code
  19. console, you can use the relevant display API for the kernel you are
  20. using. For example, the IPython kernel provides a variety of convenience
  21. classes for displaying rich output:
  22. .. code:: python
  23. from IPython.display import display, HTML
  24. display(HTML('<h1>Hello World</h1>'))
  25. Running this code will display the HTML in the output of a notebook or
  26. code console cell:
  27. .. image:: images/file_formats_html_display.png
  28. :align: center
  29. :class: jp-screenshot
  30. The IPython display function can also construct a raw rich output
  31. message from a dictionary of keys (MIME types) and values (MIME data):
  32. .. code:: python
  33. from IPython.display import display
  34. display({'text/html': '<h1>Hello World</h1>', 'text/plain': 'Hello World'}, raw=True)
  35. Other Jupyter kernels offer similar APIs.
  36. The rest of this section highlights some of the common data formats that
  37. JupyterLab supports.
  38. Markdown
  39. ~~~~~~~~
  40. - File extension: ``.md``
  41. - MIME type: ``text/markdown``
  42. Markdown is a simple and popular markup language used for text cells in
  43. the Jupyter Notebook.
  44. Markdown documents can be edited as text files or rendered inline:
  45. .. raw:: html
  46. <div class="jp-youtube-video">
  47. <iframe width="560" height="315" src="https://www.youtube-nocookie.com/embed/eQsRlqK-z1c?rel=0&amp;showinfo=0" frameborder="0" allow="autoplay; encrypted-media" allowfullscreen></iframe>
  48. </div>
  49. The Markdown syntax supported in this mode is the same syntax used in
  50. the Jupyter Notebook (for example, LaTeX equations work). As seen in the
  51. animation, edits to the Markdown source are immediately reflected in the
  52. rendered version.
  53. Images
  54. ~~~~~~
  55. - File extensions: ``.bmp``, ``.gif``, ``.jpeg``, ``.jpg``, ``.png``,
  56. ``.svg``
  57. - MIME types: ``image/bmp``, ``image/gif``, ``image/jpeg``,
  58. ``image/png``, ``image/svg+xml``
  59. JupyterLab supports image data in cell output and as files in the above
  60. formats. In the image file viewer, you can use keyboard shortcuts such
  61. as ``+`` and ``-`` to zoom the image and ``0`` to reset the zoom level.
  62. To edit an SVG image as a text file, right-click on the SVG filename in
  63. the file browser and select the “Editor” item in the “Open With”
  64. submenu:
  65. .. raw:: html
  66. <div class="jp-youtube-video">
  67. <iframe width="560" height="315" src="https://www.youtube-nocookie.com/embed/y_ydmAmVdCA?rel=0&amp;showinfo=0" frameborder="0" allow="autoplay; encrypted-media" allowfullscreen></iframe>
  68. </div>
  69. CSV
  70. ~~~
  71. - File extension: ``.csv``
  72. - MIME type: None
  73. Files with rows of comma-separated values (CSV files) are a common
  74. format for tabular data. The default viewer for CSV files in JupyterLab
  75. is a high-performance data grid viewer (which can also handle tab- and
  76. semicolon-separated values):
  77. .. raw:: html
  78. <div class="jp-youtube-video">
  79. <iframe width="560" height="315" src="https://www.youtube-nocookie.com/embed/z6xuZ9H3Imo?rel=0&amp;showinfo=0" frameborder="0" allow="autoplay; encrypted-media" allowfullscreen></iframe>
  80. </div>
  81. To edit a CSV file as a text file, right-click on the file in the file
  82. browser and select the “Editor” item in the “Open With” submenu:
  83. .. raw:: html
  84. <div class="jp-youtube-video">
  85. <iframe width="560" height="315" src="https://www.youtube-nocookie.com/embed/b5oAoVB3Wd4?rel=0&amp;showinfo=0" frameborder="0" allow="autoplay; encrypted-media" allowfullscreen></iframe>
  86. </div>
  87. JSON
  88. ~~~~
  89. - File extension: ``.json``
  90. - MIME type: ``application/json``
  91. JavaScript Object Notation (JSON) files are common in data science.
  92. JupyterLab supports displaying JSON data in cell output or viewing a
  93. JSON file using a searchable tree view:
  94. .. raw:: html
  95. <div class="jp-youtube-video">
  96. <iframe width="560" height="315" src="https://www.youtube-nocookie.com/embed/FRj1r7-7kiQ?rel=0&amp;showinfo=0" frameborder="0" allow="autoplay; encrypted-media" allowfullscreen></iframe>
  97. </div>
  98. To edit the JSON as a text file, right-click on the filename in the file
  99. browser and select the “Editor” item in the “Open With” submenu:
  100. .. raw:: html
  101. <div class="jp-youtube-video">
  102. <iframe width="560" height="315" src="https://www.youtube-nocookie.com/embed/HKcJAGZngzw?rel=0&amp;showinfo=0" frameborder="0" allow="autoplay; encrypted-media" allowfullscreen></iframe>
  103. </div>
  104. HTML
  105. ~~~~
  106. - File extension: ``.html``
  107. - MIME type: ``text/html``
  108. JupyterLab supports rendering HTML in cell output and editing HTML files
  109. as text in the file editor.
  110. LaTeX
  111. ~~~~~
  112. - File extension: ``.tex``
  113. - MIME type: ``text/latex``
  114. JupyterLab supports rendering LaTeX equations in cell output and editing
  115. LaTeX files as text in the file editor.
  116. PDF
  117. ~~~
  118. - File extension: ``.pdf``
  119. - MIME type: ``application/pdf``
  120. PDF is a common standard file format for documents. To view a PDF file
  121. in JupyterLab, double-click on the file in the file browser:
  122. .. raw:: html
  123. <div class="jp-youtube-video">
  124. <iframe width="560" height="315" src="https://www.youtube-nocookie.com/embed/vLAEzD5dxQw?rel=0&amp;showinfo=0" frameborder="0" allow="autoplay; encrypted-media" allowfullscreen></iframe>
  125. </div>
  126. Vega/Vega-Lite
  127. ~~~~~~~~~~~~~~
  128. Vega:
  129. - File extensions: ``.vg``, ``.vg.json``
  130. - MIME type: ``application/vnd.vega.v2+json``
  131. Vega-Lite:
  132. - File extensions: ``.vl``, ``.vl.json``
  133. - MIME type: ``application/vnd.vegalite.v1+json``
  134. Vega and Vega-Lite are declarative visualization grammars that enable
  135. visualizations to be encoded as JSON data. For more information, see the
  136. documentation of Vega or Vega-Lite. JupyterLab supports rendering Vega
  137. 2.x and Vega-Lite 1.x data in files and cell output.
  138. Vega-Lite 1.x files, with a ``.vl`` or ``.vl.json`` file extension, can
  139. be opened by double-clicking the file in the file browser:
  140. .. raw:: html
  141. <div class="jp-youtube-video">
  142. <iframe width="560" height="315" src="https://www.youtube-nocookie.com/embed/Dddtyz5fWkU?rel=0&amp;showinfo=0" frameborder="0" allow="autoplay; encrypted-media" allowfullscreen></iframe>
  143. </div>
  144. The files can also be opened in the JSON viewer or file editor through
  145. the “Open With…” submenu in the file browser content menu:
  146. .. raw:: html
  147. <div class="jp-youtube-video">
  148. <iframe width="560" height="315" src="https://www.youtube-nocookie.com/embed/qaiGRXh4jxc?rel=0&amp;showinfo=0" frameborder="0" allow="autoplay; encrypted-media" allowfullscreen></iframe>
  149. </div>
  150. As with other files in JupyterLab, multiple views of a single file
  151. remain synchronized, enabling you to interactively edit and render
  152. Vega/Vega-Lite visualizations:
  153. .. raw:: html
  154. <div class="jp-youtube-video">
  155. <iframe width="560" height="315" src="https://www.youtube-nocookie.com/embed/4Me4rCeS8To?rel=0&amp;showinfo=0" frameborder="0" allow="autoplay; encrypted-media" allowfullscreen></iframe>
  156. </div>
  157. The same workflow also works for Vega 2.x files, with a ``.vg`` or
  158. ``.vg.json`` file extension.
  159. Output support for Vega/Vega-Lite in a notebook or code console is
  160. provided through third-party libraries such as Altair (Python), the
  161. vegalite R package, or Vegas (Scala/Spark).
  162. .. image:: images/file_formats_altair.png
  163. :align: center
  164. :class: jp-screenshot
  165. A JupyterLab extension that supports Vega 3.x and Vega-Lite 2.x can be
  166. found `here <https://github.com/jupyterlab/jupyter-renderers>`__.
  167. Virtual DOM
  168. ~~~~~~~~~~~
  169. - File extensions: ``.vdom``, ``.json``
  170. - MIME type: ``application/vdom.v1+json``
  171. Virtual DOM libraries such as `react.js <https://reactjs.org/>`__ have
  172. greatly improved the experience of rendering interactive content in
  173. HTML. The nteract project, which collaborates closely with Project
  174. Jupyter, has created a `declarative JSON
  175. format <https://github.com/nteract/vdom>`__ for virtual DOM data.
  176. JupyterLab can render this data using react.js. This works for both VDOM
  177. files with the ``.vdom`` extension, or within notebook output.
  178. Here is an example of a ``.vdom`` file being edited and rendered
  179. interactively:
  180. .. raw:: html
  181. <div class="jp-youtube-video">
  182. <iframe width="560" height="315" src="https://www.youtube-nocookie.com/embed/fObR8xeKCJU?rel=0&amp;showinfo=0" frameborder="0" allow="autoplay; encrypted-media" allowfullscreen></iframe>
  183. </div>
  184. The `nteract/vdom <https://github.com/nteract/vdom>`__ library provides
  185. a Python API for creating VDOM output that is rendered in nteract and
  186. JupyterLab:
  187. .. image:: images/file_formats_nteract_vdom.png
  188. :align: center
  189. :class: jp-screenshot