我想从笔记本的单元格内保存笔记本的副本(或将其重命名)。
最好不要有太多的JavaScript。实际上,我猜这种形式的东西应该可以用
from IPython.display import display_html
display_html("script>Jupyter....???...()</script>")发布于 2019-04-18 18:52:20
这是一个仅用Python编写的解决方案。notebook_path函数来自P.Toccaceli在How do I get the current IPython Notebook name上的解决方案。
from notebook import notebookapp
import urllib
import json
import os
import ipykernel
from shutil import copy2
def notebook_path():
"""Returns the absolute path of the Notebook or None if it cannot be determined
NOTE: works only when the security is token-based or there is also no password
"""
connection_file = os.path.basename(ipykernel.get_connection_file())
kernel_id = connection_file.split('-', 1)[1].split('.')[0]
for srv in notebookapp.list_running_servers():
try:
if srv['token']=='' and not srv['password']: # No token and no password, ahem...
req = urllib.request.urlopen(srv['url']+'api/sessions')
else:
req = urllib.request.urlopen(srv['url']+'api/sessions?token='+srv['token'])
sessions = json.load(req)
for sess in sessions:
if sess['kernel']['id'] == kernel_id:
return os.path.join(srv['notebook_dir'],sess['notebook']['path'])
except:
pass # There may be stale entries in the runtime directory
return None
def copy_current_nb(new_name):
nb = notebook_path()
if nb:
new_path = os.path.join(os.path.dirname(nb), new_name+'.ipynb')
copy2(nb, new_path)
else:
print("Current notebook path cannot be determined.")然后,只需使用copy_current_nb('Save1')在同一目录中创建一个名为Save1.ipynb的副本。
https://stackoverflow.com/questions/55735598
复制相似问题