我不熟悉python编码,我能够使用jupyter笔记本服务器rest api创建新的.ipynb文件。
我正在使用下面的curl命令通过rest api创建一个新的.ipynb文件,
curl -X PUT "http://localhost:8890/api/contents/two.ipynb" -H "accept: application/json" -H "Authorization: Token xxxxxxxx" -d "{\"name\": \"one.ipynb\"}"
现在,我正在尝试使用jupyter notebook server rest api创建一个新的jupyter notebook cell,或者使用python代码并使用基本的print语句更新cell。但不能拿到。有人知道怎么解决这个问题吗?
提前感谢!
发布于 2020-03-16 17:21:34
我用以下代码完成了任务:创建新的notebook:
import requests
import nbformat as nbf
import sys
from nbconvert.preprocessors import ExecutePreprocessor
import json
import os
def create_nb():
api_url = 'http://192.168.1.xxx:8890/api/contents/root/ppl'
headers = {'token': 'token_passwd'}
headers1 = {'Authorization': 'Token token_passwd'}
body={"type": "notebook"}
result = requests.post(api_url,headers=headers1,json=body)
output=result.json()
return('/'+output["path"])
在指定路径下新建单元格:
def create_cell(path,session_id):
head,file_name = os.path.split(path)
file_path=path[1:]
api_url = 'http://192.168.1.126:8890/api/sessions/'+session_id
headers1 = {'Authorization': 'Token token_passwd','Content-Type': 'application/json'}
body={"name":file_name, "type": "notebook","path": file_path,"kernel": {"name": "python3"}}
#result = requests.post(api_url,headers=headers1,json=body)
present_id=requests.get(api_url,headers=headers1)
#print("present_id ---->>> "+present_id.json()["id"])
nb1 = nbf.read(path, as_version=4)
code=""""""
nb1['cells']= nb1['cells']+[nbf.v4.new_code_cell(code)]
with open(path, "w") as f:
nbf.write(nb1, f)
return("cell created")
https://stackoverflow.com/questions/58079840
复制相似问题