Как скачать файл из jupiter notebook
Перейти к содержимому

Как скачать файл из jupiter notebook

  • автор:

Jupyter/iPython Notebook download files as CSV, zip

If you need to keep the Jupyter Notebook output data for some reason and download it as a file csv/zip(for larger files)/.ipynb(all cells) on your local machine then you have several options depending on the server and configuration. If tried to investigate all possible ways — the result is in this post:

Download the whole notebook

This is useful when you want to download the whole notebook with all cells, outputs and states. Sometimes the files are too big and this method is not convenient or you may need only a specific output file. In this case you can check the next section.

For Jupyter notebook you can download the notebooks as .ipynb then you can:

  • Open your notebook in Jupyter
  • Click File
  • Download as
  • Choose format:
    • Notebook (.ipynb)
    • Python (.py)
    • HTML (.html)
    • Markdown (.md)
    • LaTeX (.tex)
    • PDF via LaTeX (.pdf)

    You can test it on the online demonstration here: Welcome to Jupyter!

    jupyter_download

    Python 3 Download CSV from an Jupyter Notebook

    This is an update for Python 3. If you need to download a file:

    From your Jupyter server and you can’t use FileLinks because of some limitations of the server then you can use the next code example. Where:

    • title = "Download CSV file" — is the name which is going to be shown in the browser as download link
    • filename = "data.csv" — this is the name for the downloaded file
    • file = ‘/tmp/report/stats/hours.txt’ — this is the absolute location of the file which you want to download (from the server)

    You can download text and CSV files.

    If you like to check the files on the server than you can use jupyter magics like:

    Create and download CSV/zip file

    In this section you will see how to create a single output file in Jupyter and download the file as CSV or zip depending on the size. Note that some browsers will work only with small sized dataframes. For example Chrome’s max data URI size is 2MB. In this case, this is after Base64 encoding.

    For the simple and small data frame which can be download as CSV file you can use:

    This will not work for larger dataframes. In this case you can zip your file and download it as a zip. This ensures working with a large DataFrame without the need of server configurations or additional setup. This is the example which allows you creating csv or zip file with Jupyter/iPython Notebook:

    the usage(in the cell) for csv and zip is:

    Direct download with FileLinks(‘/path/to/’)

    Assuming that you have the files stored on the server and proper configuration to download files then you can use this python code in order to get any file from the server. If the configuration is not correct you will be redirected to wrong address and the file will be unavailable for download:

    In order to ensure that the files can be downloaded from the server you may need to set up a small web server with python. You can see more information here: Ubuntu 18 Start simple web host with python on localhost — SoftHints. In short this is:

    Using extension to download folders

    This is extension — nbzip — provides a button to zip and download a jupyter server folder. You can check the latest version here:

    In order to enable it:

    • install it by:
    • enable it by:
    • You can test that you have a new button next to the apply button which is downloading the folder content as an archive.

    Download files with Linux command

    Another option for downloading information from your jupyter server if the files are stored on the server is by linux commands like:

    and then using python code to create downloadable link:

    or any other way to download information from your server like scp:

    https://amdy.su/wp-admin/options-general.php?page=ad-inserter.php#tab-8

    By using SoftHints — Python, Linux, Pandas , you agree to our Cookie Policy.

    How can I download (save) all the codes from Jupyter Notebook?

    I have written many programs in Jupyter Notebook. I have been downloading manually in HTML and python format. I would like to ask if there is a faster way to download all the programs that I have written there in a folder.

    4 Answers 4

    Jupyter notebook is a JSON file.

    You can open it as a textual file and save all cells which have a code as the cell_type .

    You can do it pretty easily from the notebooks file menu.

    towhid's user avatar

    1. Go to your working directory
    2. Run this command in cmd $ ipython nbconvert —to FORMAT notebook.ipynb .This will convert the IPython document file notebook.ipynb into the output format given by the FORMAT string.

    The default output format is HTML, for which the —to the argument may be omitted:

    Downloading files from Jupyter Notebook

    You’ve done an analysis and generated an output file in a Jupyter notebook. How do you get it down to your computer? For a local server you could find it in your filesystem, or for a remote server copy it with something like scp. But there are easier ways.

    You can download individual files from the file navigator (which you can get to by clicking on the Jupyter icon in the top left corner). You just need to click the checkbox next to the file you want to download and then click the “download” button at the top of the pane. The only drawback is it only works for individual files; not directories or multiple files.

    Download interface in Jupter

    An even easier way in a Python notebook is to use FileLink:

    then a link will display below the cell.

    If you want links to multiple files in a single cell you need to use display to show them all (otherwise it only gets shown if it’s the last line of the cell):

    What if you want to download multiple files or a whole directory? You don’t really want to have to click “download” a whole bunch of times. But you can easily put them all in a zipfile in Python, and then download the single zipfile as before. For example if you wanted to download all the csvs in the current directory:

    If you want to download a whole directory there’s an even easier way with shutil.make_archive .

    Then you can download the files very quickly via FileLink without ever leaving the Jupyter notebook.

    4 ways to export Jupyter Notebook as PDF

    Have you ever wanted to export your Jupyter Notebook to PDF file? The PDF is an abbreviation for Portable Document Format. It can be displayed on any operating system. That makes it format of choice for many who wants to share their results. In this post I will show you 4 different ways how to export Jupyter Notebook as PDF file.

    1. Print Jupyter Notebook to PDF

    The most straightforward way is just to use web browser feature of print to PDF. There is no need to install any additional packages.

    The big advantage of this approach is that we don’t need to install additional libraries to make it works! However, it is manual approach — hard to automate. What is more, we can’t control the process of export (for example, we can’t hide code in the exported file).

    2. Download Jupyter Notebook as PDF

    The Jupyter Notebook has an option to export the notebook to many formats. It can be accessed by clicking File -> Download as -> PDF via LaTeX (or PDF via HTML — not visible in the screenshot).

    Print Jupyter Notebook to PDF

    This approach requires you to install some additional packages. For me, the option that exports with LaTeX is difficult. It requires you to install much more packages than option of export via HTML. If you don’t have required packages you will get the error message:

    Jupyter Notebook error when exporting to pdf

    This is scary, isn’t it? The option to export via HTML requires you to download Chromium (headless browser).

    I rarely use this approach to be honest. It requires additional installation (that is not trivial) and is manual. This approach under the hood is using nbconvert tool.

    3. Export to PDF with nbconvert

    That’s one of my favourite approaches. Why? Because it is a command line tool that can be used in the scripts 🙂

    Still you need to install additional packages to make it work. The same as in Jupyter Notebook GUI there are two options:

    • export to PDF via LaTex,
    • export to PDF via HTML.

    The latter is my choice. It requires pyppeteer and Chromium to be download. This can be easily achieved by running:

    and adding a flag —allow-chromium-download when converting a notebook:

    The flag should be added only one time. It is not necessary after Chromium installation. The nbconvert has many optional arguments that control the export. For example, you can easily hide the code with —no-input flag:

    Additionally, you can apply more options to the export like removing selected cells or change the templates.

    4. Share Notebook with Mercury

    There is an open-source framework Mercury that makes Python notebooks sharing painless. It converts notebooks to interactive documents (web apps, reports, slides, dashboards). You can share a notebook with interactive widgets. Your end-users can tweak widgets values and execute the notebook with new values (without changing the code). The Mercury allows to export the executed notebook into standalone HTML or PDF file. The end-user just need to clik the Download button.

    Summary

    Saving notebooks to PDF is a great way to persist results in a shareble format. PDFs can be easily published online or send in the email. There are several ways to convert Jupyter Notebook as PDF. The automatic conversion can be easily achieved with nbconvert tool. Notebooks shared with Mercury framework can be easily converted to PDF. The PDF notebook can be manually downloaded from the website.

Добавить комментарий

Ваш адрес email не будет опубликован. Обязательные поля помечены *