Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Load local data into IPython notebook server

Tags:

server

ipython

I did setup an ipython server for other people (in my company department) to have a chance to learn and work with python.

Now I wonder how people can load their own local data into the ipython notebook session on the remote server. Is there any way to do this?

like image 904
Michael Hecht Avatar asked Apr 30 '15 13:04

Michael Hecht


People also ask

How do I load files into IPython?

Starting from IPython 3 (now Jupyter project), the notebook has a text editor that can be used as a more convenient alternative to load/edit/save text files. A text file can be loaded in a notebook cell with the magic command %load. If you execute a cell as shown below. Hope this is helpful!

How do I run IPython notebook locally?

Launch a Notebook To launch a Jupyter notebook, open your terminal and navigate to the directory where you would like to save your notebook. Then type the command jupyter notebook and the program will instantiate a local server at localhost:8888 (or another specified port).


Video Answer


2 Answers

Since you have jupyter installed, all users should see the files/folders in the jupyter startup directory as well as its subdirectory. The new button on the jupyter notebook can be used to create a new file/folder or even a terminal. Files can be uploaded using drag-drop or click here feature highlighted below.

enter image description here

like image 86
Amit Verma Avatar answered Oct 08 '22 01:10

Amit Verma


An alternative way to achieve this with python:

def jupyter_upload(token, filePath, resourceDstPath, jupyterUrl='http://localhost:8888'):
    """
        Uploads File to Jupyter Notebook Server
        ----------------------------------------
        :param token:
            The authorization token issued by Jupyter for authentification 
            (enabled by default as of version 4.3.0)
        :param filePath:
            The file path to the local content to be uploaded

        :param resourceDstPath:
            The path where resource should be placed.
            The destination directory must exist.

        :param jupyterUrl:
            The url to the jupyter server. Default value is typical localhost installation.

        :return: server response

    """
    import os
    import base64
    import urllib
    import json
    import requests
    dstPath = urllib.quote(resourceDstPath)
    dstUrl = '%s/api/contents/%s' % (jupyterUrl, dstPath)
    fileName = filePath[1 + filePath.rfind(os.sep):]
    headers = {}
    headers['Authorization'] = 'token '+token
    with open(filePath, 'r') as myfile:
        data=myfile.read()
        b64data=base64.encodestring(data)
        body = json.dumps({
            'content':b64data,
            'name': fileName,
            'path': resourceDstPath,
            'format': 'base64',
            'type':'file'
        })
        return requests.put(dstUrl, data=body, headers=headers, verify=True)
like image 10
volhv Avatar answered Oct 08 '22 00:10

volhv