file_formats.rst 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282
  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 by default. JupyterLab extensions can also add support for
  38. other file formats.
  39. .. _markdown:
  40. Markdown
  41. ~~~~~~~~
  42. - File extension: ``.md``
  43. - MIME type: ``text/markdown``
  44. Markdown is a simple and popular markup language used for text cells in
  45. the Jupyter Notebook.
  46. Markdown documents can be edited as text files or rendered inline:
  47. .. raw:: html
  48. <div class="jp-youtube-video">
  49. <iframe src="https://www.youtube-nocookie.com/embed/eQsRlqK-z1c?rel=0&amp;showinfo=0" frameborder="0" allow="autoplay; encrypted-media" allowfullscreen></iframe>
  50. </div>
  51. The Markdown syntax supported in this mode is the same syntax used in
  52. the Jupyter Notebook (for example, LaTeX equations work). As seen in the
  53. animation, edits to the Markdown source are immediately reflected in the
  54. rendered version.
  55. Images
  56. ~~~~~~
  57. - File extensions: ``.bmp``, ``.gif``, ``.jpeg``, ``.jpg``, ``.png``,
  58. ``.svg``
  59. - MIME types: ``image/bmp``, ``image/gif``, ``image/jpeg``,
  60. ``image/png``, ``image/svg+xml``
  61. JupyterLab supports image data in cell output and as files in the above
  62. formats. In the image file viewer, you can use keyboard shortcuts such
  63. as ``+`` and ``-`` to zoom the image, ``[`` and ``]`` to rotate the image,
  64. and ``H`` and ``V`` to flip the image horizontally and vertically. Use
  65. ``I`` to invert the colors, and use ``0`` to reset the image.
  66. To edit an SVG image as a text file, right-click on the SVG filename in
  67. the file browser and select the “Editor” item in the “Open With”
  68. submenu:
  69. .. raw:: html
  70. <div class="jp-youtube-video">
  71. <iframe src="https://www.youtube-nocookie.com/embed/y_ydmAmVdCA?rel=0&amp;showinfo=0" frameborder="0" allow="autoplay; encrypted-media" allowfullscreen></iframe>
  72. </div>
  73. .. _csv:
  74. CSV
  75. ~~~
  76. - File extension: ``.csv``
  77. - MIME type: None
  78. Files with rows of comma-separated values (CSV files) are a common format for
  79. tabular data. The default viewer for CSV files in JupyterLab is a
  80. high-performance data grid viewer (which can also handle tab- and
  81. semicolon-separated values). JupyterLab can open files up to the maximum string
  82. size in the browser (which ranges from approximately 250MB to 2GB, depending on
  83. the browser):
  84. .. raw:: html
  85. <div class="jp-youtube-video">
  86. <iframe src="https://www.youtube-nocookie.com/embed/z6xuZ9H3Imo?rel=0&amp;showinfo=0" frameborder="0" allow="autoplay; encrypted-media" allowfullscreen></iframe>
  87. </div>
  88. To edit a CSV file as a text file, right-click on the file in the file
  89. browser and select the “Editor” item in the “Open With” submenu:
  90. .. raw:: html
  91. <div class="jp-youtube-video">
  92. <iframe src="https://www.youtube-nocookie.com/embed/b5oAoVB3Wd4?rel=0&amp;showinfo=0" frameborder="0" allow="autoplay; encrypted-media" allowfullscreen></iframe>
  93. </div>
  94. JSON
  95. ~~~~
  96. - File extension: ``.json``
  97. - MIME type: ``application/json``
  98. JavaScript Object Notation (JSON) files are common in data science.
  99. JupyterLab supports displaying JSON data in cell output or viewing a
  100. JSON file using a searchable tree view:
  101. .. raw:: html
  102. <div class="jp-youtube-video">
  103. <iframe src="https://www.youtube-nocookie.com/embed/FRj1r7-7kiQ?rel=0&amp;showinfo=0" frameborder="0" allow="autoplay; encrypted-media" allowfullscreen></iframe>
  104. </div>
  105. To edit the JSON as a text file, right-click on the filename in the file
  106. browser and select the “Editor” item in the “Open With” submenu:
  107. .. raw:: html
  108. <div class="jp-youtube-video">
  109. <iframe src="https://www.youtube-nocookie.com/embed/HKcJAGZngzw?rel=0&amp;showinfo=0" frameborder="0" allow="autoplay; encrypted-media" allowfullscreen></iframe>
  110. </div>
  111. HTML
  112. ~~~~
  113. - File extension: ``.html``
  114. - MIME type: ``text/html``
  115. JupyterLab supports rendering HTML in cell output and editing HTML files
  116. as text in the file editor.
  117. LaTeX
  118. ~~~~~
  119. - File extension: ``.tex``
  120. - MIME type: ``text/latex``
  121. JupyterLab supports rendering LaTeX equations in cell output and editing
  122. LaTeX files as text in the file editor.
  123. PDF
  124. ~~~
  125. - File extension: ``.pdf``
  126. - MIME type: ``application/pdf``
  127. PDF is a common standard file format for documents. To view a PDF file
  128. in JupyterLab, double-click on the file in the file browser:
  129. .. raw:: html
  130. <div class="jp-youtube-video">
  131. <iframe src="https://www.youtube-nocookie.com/embed/vLAEzD5dxQw?rel=0&amp;showinfo=0" frameborder="0" allow="autoplay; encrypted-media" allowfullscreen></iframe>
  132. </div>
  133. .. _vega-lite:
  134. Vega/Vega-Lite
  135. ~~~~~~~~~~~~~~
  136. Vega:
  137. - File extensions: ``.vg``, ``.vg.json``
  138. - MIME type: ``application/vnd.vega.v2+json``
  139. Vega-Lite:
  140. - File extensions: ``.vl``, ``.vl.json``
  141. - MIME type: ``application/vnd.vegalite.v1+json``
  142. Vega and Vega-Lite are declarative visualization grammars that enable
  143. visualizations to be encoded as JSON data. For more information, see the
  144. documentation of Vega or Vega-Lite. JupyterLab supports rendering Vega
  145. 2.x and Vega-Lite 1.x data in files and cell output.
  146. Vega-Lite 1.x files, with a ``.vl`` or ``.vl.json`` file extension, can
  147. be opened by double-clicking the file in the file browser:
  148. .. raw:: html
  149. <div class="jp-youtube-video">
  150. <iframe src="https://www.youtube-nocookie.com/embed/Dddtyz5fWkU?rel=0&amp;showinfo=0" frameborder="0" allow="autoplay; encrypted-media" allowfullscreen></iframe>
  151. </div>
  152. The files can also be opened in the JSON viewer or file editor through
  153. the “Open With…” submenu in the file browser content menu:
  154. .. raw:: html
  155. <div class="jp-youtube-video">
  156. <iframe src="https://www.youtube-nocookie.com/embed/qaiGRXh4jxc?rel=0&amp;showinfo=0" frameborder="0" allow="autoplay; encrypted-media" allowfullscreen></iframe>
  157. </div>
  158. As with other files in JupyterLab, multiple views of a single file
  159. remain synchronized, enabling you to interactively edit and render
  160. Vega/Vega-Lite visualizations:
  161. .. raw:: html
  162. <div class="jp-youtube-video">
  163. <iframe src="https://www.youtube-nocookie.com/embed/4Me4rCeS8To?rel=0&amp;showinfo=0" frameborder="0" allow="autoplay; encrypted-media" allowfullscreen></iframe>
  164. </div>
  165. The same workflow also works for Vega 2.x files, with a ``.vg`` or
  166. ``.vg.json`` file extension.
  167. Output support for Vega/Vega-Lite in a notebook or code console is
  168. provided through third-party libraries such as Altair (Python), the
  169. vegalite R package, or Vegas (Scala/Spark).
  170. .. image:: images/file_formats_altair.png
  171. :align: center
  172. :class: jp-screenshot
  173. A JupyterLab extension that supports Vega 3.x and Vega-Lite 2.x can be
  174. found `here <https://github.com/jupyterlab/jupyter-renderers>`__.
  175. Virtual DOM
  176. ~~~~~~~~~~~
  177. - File extensions: ``.vdom``, ``.json``
  178. - MIME type: ``application/vdom.v1+json``
  179. Virtual DOM libraries such as `react.js <https://reactjs.org/>`__ have
  180. greatly improved the experience of rendering interactive content in
  181. HTML. The nteract project, which collaborates closely with Project
  182. Jupyter, has created a `declarative JSON
  183. format <https://github.com/nteract/vdom>`__ for virtual DOM data.
  184. JupyterLab can render this data using react.js. This works for both VDOM
  185. files with the ``.vdom`` extension, or within notebook output.
  186. Here is an example of a ``.vdom`` file being edited and rendered
  187. interactively:
  188. .. raw:: html
  189. <div class="jp-youtube-video">
  190. <iframe src="https://www.youtube-nocookie.com/embed/fObR8xeKCJU?rel=0&amp;showinfo=0" frameborder="0" allow="autoplay; encrypted-media" allowfullscreen></iframe>
  191. </div>
  192. The `nteract/vdom <https://github.com/nteract/vdom>`__ library provides
  193. a Python API for creating VDOM output that is rendered in nteract and
  194. JupyterLab:
  195. .. image:: images/file_formats_nteract_vdom.png
  196. :align: center
  197. :class: jp-screenshot