Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Retrieving files from remote IPython notebook server?

Tags:

If I don't want to give out SSH access to users of my remote IPython notebook server. Is there a way to let users browse non .ipynb files and download them?

like image 345
user1234310 Avatar asked Jun 26 '14 18:06

user1234310


People also ask

How do I access files in Jupyter notebook?

Open Jupyter Notebook Files You can open existing Jupyter Notebook files (. ipynb) in the Jupyter Notebook dashboard by clicking on the name of the file in the dashboard (e.g. filename. ipynb ).

Where files from Jupyter notebooks are saved?

Configuration files Config files are stored by default in the ~/. jupyter directory.


2 Answers

You can use FileLink and FileLinks that are built-in:

from IPython.display import FileLink, FileLinks FileLinks('.') #lists all downloadable files on server 

The code above generates:

./ some_python_file.py some_xml_file.xml some_ipynb_file.ipynb 

The three items above are links that you can click to download.

Click here for an example from ipython.org

like image 69
shad Avatar answered Sep 18 '22 05:09

shad


Unfortunately due to edge case reasons, FileLink doesn't work for files outside the Jupyter directory. You can however get around this by creating a link to the file first:

os.symlink( file_name, "tmp.txt" ) display( FileLink( "tmp.txt" ) ) 

In reality the workaround above leaves the link on disk. It also assumes everything has a "txt" extension. We probably want to clean up previous links to avoid cluttering the directory, and not rename the downloaded file. Here is a solution that persists only one link at at time and retains the base name:

def download_file( file_name : str ) -> None:     import os     from IPython.display import display, FileLink          base_name : str = os.path.basename( file_name )     k_info_file : str = ".download_file_info.txt"          # Remove previous link     if os.path.isfile( k_info_file ):         with open( k_info_file, "r" ) as fin:             previous_file = fin.read()                  if os.path.isfile( previous_file ):             print( "Removing previous file link." )             os.remove( previous_file )          # Remember current link     with open( k_info_file, "w" ) as fout:         fout.write( base_name )          # Create the link     assert not os.path.isfile( base_name ), "Name in use."     os.symlink( file_name, base_name )          # Return the link     display( FileLink( base_name ) ) 
like image 45
c z Avatar answered Sep 21 '22 05:09

c z