[英]How to create and open a jupyter notebook ipynb file directly from terminal
The command jupyter notebook
will open the Jupyter directory tree page where you can create a ipynb file.命令
jupyter notebook
将打开 Jupyter 目录树页面,您可以在其中创建 ipynb 文件。
Is there a way to skip that page and create and open the ipynb file directly on the browser?有没有办法跳过该页面并直接在浏览器上创建和打开 ipynb 文件?
I was thinking something like jupyter notebook mynotebook.ipynb我在想像 jupyter notebook mynotebook.ipynb 这样的东西
Open notebook in browser在浏览器中打开笔记本
jupyter notebook <notebook>.ipynb
Create empty, minimal notebook:创建空的、最小的笔记本:
"""create-notebook.py
Creates a minimal jupyter notebook (.ipynb)
Usage: create-notebook <notebook>
"""
import sys
from notebook import transutils as _
from notebook.services.contents.filemanager import FileContentsManager as FCM
try:
notebook_fname = sys.argv[1].strip('.ipynb')
except IndexError:
print("Usage: create-notebook <notebook>")
exit()
notebook_fname += '.ipynb' # ensure .ipynb suffix is added
FCM().new(path=notebook_fname)
Alias create-notebook script:别名 create-notebook 脚本:
alias create-notebook='python $(pwd)/create-notebook.py'
create-notebook my_notebook && jupyter notebook my_notebook.ipynb
This can't be the most desirable method.这不是最理想的方法。 Maybe there's an option native to Jupyter or theextensions , but I haven't come across one, nor have I come across the need to do this.
也许有一个 Jupyter 或extensions原生的选项,但我没有遇到过,也没有遇到过这样做的需要。 The documentation suggests the developers are encouraging users to land at the dashboard.
该文档表明开发人员正在鼓励用户登陆仪表板。
Start with an empty notebook Untitled.ipynb
.从一个空的 Notebook
Untitled.ipynb
。 To generate it, save the default file that is created when you create a new notebook from the jupyter dashboard.要生成它,请保存从 jupyter 仪表板创建新笔记本时创建的默认文件。 This empty notebook will be used as a template for creating new, empty notebooks at the command line.
这个空笔记本将用作在命令行中创建新的空笔记本的模板。 The contents of
Untitled.ipynb
for me, jupyter version 4.4.0, look like this: Untitled.ipynb
的内容对我来说,jupyter 版本 4.4.0,是这样的:
$ cat Untitled.ipynb
{
"cells": [],
"metadata": {},
"nbformat": 4,
"nbformat_minor": 2
}
The file contains the bare minimum needed to launch a notebook using jupyter notebook Untitled.ipynb
(and eventually mynotebook.ipynb
), any less and it will raise a NotJSONError
.该文件包含使用
jupyter notebook Untitled.ipynb
(以及最终mynotebook.ipynb
)启动笔记本所需的最低限度,如果更少,它将引发NotJSONError
。 You can add some metadata to the template if you want to include a default kernel.如果要包含默认内核,可以向模板添加一些元数据。
From here, use command substitution to open a new, empty notebook from the command line where Untitled.ipynb
is the path to the template notebook created above and mynotebook.ipynb
is the name of the notebook you wish to create:从这里,使用命令替换从命令行打开一个新的空笔记本,其中
Untitled.ipynb
是上面创建的模板笔记本的路径,而mynotebook.ipynb
是您要创建的笔记本的名称:
$ jupyter notebook $(cat Untitled.ipynb >mynotebook.ipynb && echo mynotebook.ipynb)
You may try below command.您可以尝试以下命令。
jupyter nbconvert --to notebook --execute mynotebook.ipynb
According to Jupyter nbconvert manual below, nbconvert
with --execute
command supports running a notebook.根据Jupyter nbconvert手册下面,
nbconvert
与--execute
命令支持运行的笔记本。
I had the same pain point and created nbplot
to fix it: https://github.com/nburrus/nbplot .我有同样的痛点,并创建了
nbplot
来修复它: https : //github.com/nburrus/nbplot 。 It was designed to quickly plot files in a notebook from the command line, but overall it's just a tiny tool to generate notebooks from templates and open them in the browser.它旨在从命令行快速在笔记本中绘制文件,但总的来说,它只是一个从模板生成笔记本并在浏览器中打开它们的小工具。 Here is how you would use it with the included empty template to answer your question:
以下是如何将它与包含的空模板一起使用来回答您的问题:
pip3 install --upgrade nbplot
nbplot -t empty -o mynotebook.ipynb
It will try to be smart about reusing existing notebook servers instead of always starting a new one, and it's easy to add custom notebook templates to ~/.nbplot/
if you don't want to start with an empty notebook.它将尝试巧妙地重用现有的笔记本服务器,而不是总是启动一个新的服务器,如果您不想从一个空的笔记本开始,可以很容易地将自定义笔记本模板添加到
~/.nbplot/
。
Jupyter will start running on Tornado server on localhost. Jupyter 将开始在本地主机上的 Tornado 服务器上运行。 The link is something like http://localhost/Tree When you open a notebook, this is done in another page.
该链接类似于http://localhost/Tree当您打开笔记本时,这是在另一个页面中完成的。 You can try to write a batch script to call kupyter notebok, then call your browser with the address to your notebook.
你可以尝试写一个批处理脚本来调用kupyter notebok,然后用你的notebook的地址调用你的浏览器。 I take it that if the notebook doesn't exist, is not created, then it will not work (the page is note created, it cannot be opened).
我认为如果笔记本不存在,未创建,则它将无法工作(页面已创建,无法打开)。
You can create a new notebook from the command line using jupytext
:您可以使用
jupytext
从命令行创建一个新笔记本:
# create python file
touch foo.py
# add kernel information
jupytext --set-kernel - foo.py
# convert to notebook
jupytext --to notebook foo.py
# open in browser
jupyter notebook foo.ipynb
https://jupytext.readthedocs.io/en/latest/using-cli.html https://jupytext.readthedocs.io/en/latest/using-cli.html
These commands could be wrapped in a shell script named something like jupyinit
:这些命令可以包装在一个名为
jupyinit
的 shell 脚本中:
#!/bin/bash
# Create a new Jupyter notebook from the command line
#
# Examples
# jupyinit env_name py_file.py py_file.ipynb
touch $2
jupytext --set-kernel $1 $2
jupytext --to notebook --execute $2
jupytext --set-formats ipynb,py $3
The OP's example could then be created with:然后可以使用以下命令创建 OP 的示例:
$ jupyinit myenv mynotebook.py mynotebook.ipynb
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.