Operate Jupyter with REST API to extract and save Python code

I wanted to execute the Python code of jupyter notebook on another machine, so I decided to extract and save it using the API of jupyter.

environment

Examine Jupyter tokens

Since you need a token to operate jupyter from the outside, first check the token.

I entered it when I started jupyter for the first time (should I?), But I think that I usually forgot it, so check the jupyter container in the order of search → log display → token confirmation.

Hit the Jupyter API from the outside

Notebook status

I created a notebook for testing. The URL is below. http://localhost:8888/notebooks/work/test1.ipynb image.png

Execution environment on the side that hits the API

Since API operation is performed with python, the requests package is required. It doesn't have to be Docker because it only puts requests, but I think it will be easy to make it a service later, so I will put it in a container. Details of the trial environment (Dockerfile) below.

FROM python:3.6-buster
RUN pip install requests

Build.

$ docker build -t jupyter-api .

Start the container. You don't have to mount (-v), but just mount the current to retrieve the created file.

$ docker run -it --net host -v $(pwd):/tmp jupyter-api bash

API trial

First, check if the jupyter API can be used.

import requests
url_api = 'http://localhost:8888/api'
response = requests.get(url_api)
print(response.status_code, response.text)  # 200 {"version": "6.0.3"}

It seems that I can hit the API safely, so I get a Notebook.

notebook_path = '/work/test1.ipynb'
url_file = url_api + '/contents' + notebook_path

token = 'xxxxxxxxxxxxxxxxx'   #Set token
headers =  {'Authorization': 'token ' + token}

response = requests.get(url_file, headers=headers)

Notebook information is returned in json format in response.text. Below, the execution result of print (response.text) is formatted for display.

{
  "name": "test1.ipynb",
  "path": "work/test1.ipynb",
  "last_modified": "2020-03-07T21:08:58.897321Z",
  "created": "2020-03-07T21:08:57.594298Z",
  "content": {
    "cells": [
      {
        "cell_type": "code",
        "execution_count": 1,
        "metadata": {
            "trusted": true
        },
        "outputs": [
          {
            "data": {
              "text/plain": "array([0.83709745, 0.46685874, 0.94285637, 0.03938868, 0.79617107,\n       0.98784776, 0.27798577, 0.96118447, 0.5253161 , 0.0690074 ])"
            },
            "execution_count": 1,
            "metadata": {},
            "output_type": "execute_result"
          }
        ],
        "source": "import numpy as np\nnp.random.random(10)"
      }
    ],
    "metadata": {
      "kernelspec": {
        "display_name": "Python 3",
        "language": "python",
        "name": "python3"
      },
      "language_info": {
        "codemirror_mode": {
          "name": "ipython",
          "version": 3
        },
        "file_extension": ".py",
        "mimetype": "text/x-python",
        "name": "python",
        "nbconvert_exporter": "python",
        "pygments_lexer": "ipython3",
        "version": "3.7.4"
      }
    },
    "nbformat": 4,
    "nbformat_minor": 4
  },
  "format": "json",
  "mimetype": null,
  "size": 921,
  "writable": true,
  "type": "notebook"
}

This time, I want to extract the code part of the notebook, so I can extract the source of the block of content.cells.cell_type ='code'. If cell_type is a markdown block, cell_type ='markdown'.

import json
json_src = json.loads(response.text)
src = [cell['source'] for cell in json_src['content']['cells'] if cell['cell_type'] == 'code']

The code of each cell is stored in src as an array element, and since there is only one cell in the source code this time, it is an array of one element.

['import numpy as np\nnp.random.random(10)']                                      

Output to a file. For the time being, output it with a UTF8 declaration.

with open('/tmp/test1.py', 'w') as f:
    print("# -*- coding: utf-8 -*-", file=f)
    for block in src:
        print(block, file=f)

Check the output file. Perfect.

# -*- coding: utf-8 -*-
import numpy as np
np.random.random(10)

I was able to output it safely from the Notebook, so it seems that I can boil it, bake it, or execute it.

Reference material

-Command to check token after starting jupyter with docker I referred to how to check the token.

Recommended Posts

Operate Jupyter with REST API to extract and save Python code
How to build Python and Jupyter execution environment with VS Code
Try to operate DB with Python and visualize with d3
How to operate Discord API with Python (bot registration)
Easy to use Nifty Cloud API with botocore and python
The first API to make with python Djnago REST framework
Specification generation and code generation in REST API development (Python edition)
Operate Nutanix with REST API Part 2
Try to operate Facebook with Python
A story about adding a REST API to a daemon made with Python
Correspondence analysis of sentences with COTOHA API and save to file
Image upload & download to Azure Storage. With Python + requests + REST API
Operate Firefox with Selenium from python and save the screen capture
Create a REST API to operate dynamodb with the Django REST Framework
Fractal to make and play with Python
Linking python and JavaScript with jupyter notebook
Operate home appliances with Python and IRKit
Challenge to create time axis list report with Toggl API and Python
I tried to get the authentication code of Qiita API with Python.
Sample code to get the Twitter API oauth_token and oauth_token_secret in Python 2.7
Python code to train and test with Custom Vision of Cognitive Service
Scraping tabelog with python and outputting to CSV
MessagePack-Try to link Java and Python with RPC
Interactively display algebraic curves with Python and Jupyter
Get Gmail subject and body with Python and Gmail API
List of Python code to move and remember
[Python] Mention to multiple people with Slack API
Get users belonging to your organization from Garoon REST API with Python + Requests
I tried to automate internal operations with Docker, Python and Twitter API + bonus
Ssh connect to Cisco Catalyst with CentOS7 + Python3 + netmiko and save config locally
How to use Service Account OAuth and API with Google API Client for python
Extract "current date only" and "current date and time" with python datetime.
Procedure to load MNIST with python and output to png
[First API] Try to get Qiita articles with Python
I tried to read and save automatically with VOICEROID2 2
Python Ver. To introduce WebPay with a little code.
I want to handle optimization with python and cplex
Rewrite Python2 code to Python3 (2to3)
Streaming Python and SensorTag, Kafka, Spark Streaming-Part 5: Connecting from Jupyter to Spark with Apache Toree
Extract images and tables from pdf with python to reduce the burden of reporting
Operate Kinesis with Python
I tried to automatically read and save with VOICEROID2
Connect realsense D435 to a PC with ubuntu 16.04 installed and save depth videos with python
How to extract null values and non-null values with pandas
Crawling with Python and Twitter API 1-Simple search function
[Note] How to write QR code and description in the same image with python
Operate Blender with Python
[Python] Get user information and article information with Qiita API
Save lists, dictionaries and tuples to external files python
YOLP: Extract latitude and longitude with Yahoo! Geocoder API.
Something to enjoy with Prim Pro (X-Play) and Python
[Automation] Operate GitLab with Python to facilitate inquiry management
Operate Excel with Python (1)
Operate Excel with Python (2)
How to parse Java source code with AST (Abstract Syntax Tree) using ANTLR and Python
[Python / Ruby] Understanding with code How to get data from online and write it to CSV
Try hitting the Twitter API quickly and easily with Python
screen and split screen with python and ssh login to remote server
Let's run jupyter natively supported by VS Code with python3.8
How to extract any appointment in Google Calendar with Python
kobitonote.py --Synchronize and save items edited with Kobito to Evernote