jupyter 运行 产生带单引号的无效 JSON
jupyter run produces invalid JSON with single quotes
我有一个简单的 jupyter notebook 文件 (Untitled.ipynb
),它开始于:
{
"cells": [
{
"cell_type": "code",
"execution_count": 1,
"metadata": {},
我想 运行 它并将 运行ned 版本转换为 PDF。第 1 步,运行:
jupyter run Untitled.ipynb > U-Run.ipynb
这让我得到了一些在任何地方都使用单引号的东西,jupyter nbconvert
barfs on:
$ head U-Run.ipynb
{'cells': [{'cell_type': 'code',
'execution_count': 1,
'metadata': {},
'outputs': [{'name': 'stdout', 'output_type': 'stream', 'text': ['3\n']}],
当我尝试转换为 PDF 时:
$ jupyter nbconvert --to pdf U-Run.ipynb
[NbConvertApp] Converting notebook U-Run.ipynb to pdf
Traceback (most recent call last):
File "/usr/lib/python3/dist-packages/nbformat/reader.py", line 14, in parse_json
nb_dict = json.loads(s, **kwargs)
File "/usr/lib/python3.8/json/__init__.py", line 357, in loads
return _default_decoder.decode(s)
File "/usr/lib/python3.8/json/decoder.py", line 337, in decode
obj, end = self.raw_decode(s, idx=_w(s, 0).end())
File "/usr/lib/python3.8/json/decoder.py", line 353, in raw_decode
obj, end = self.scan_once(s, idx)
json.decoder.JSONDecodeError: Expecting property name enclosed in double quotes: line 1 column 2 (char 1)
然后回溯结束:
nbformat.reader.NotJSONError: Notebook does not appear to be JSON: '{\'cells\': [{\'cell_type\': \'code\',\...
如果我从一个普通的 python 会话中导入 json
并尝试一个简单的测试,我会得到双引号:
>>> json.dumps(dict(a=1))
'{"a": 1}'
所以我不明白为什么 jupyter run
输出单引号 "json".
所有东西的版本是:
$ jupyter --version
jupyter core : 4.6.3
jupyter-notebook : 6.3.0
qtconsole : 5.0.3
ipython : 7.29.0
ipykernel : 6.5.0
jupyter client : 6.1.12
jupyter lab : 3.0.0
nbconvert : 6.0.7
ipywidgets : 7.6.3
nbformat : 5.0.4
traitlets : 5.1.1
Python: 3.8.10
Ubuntu 20.04
Apparently,也可以告诉nbconvert
到--execute
:
jupyter nbconvert --to pdf --execute U-Run.ipynb
您还可以使用papermill
执行ipynb文件,然后将输出转换为PDF:
papermill U-Run.ipynb U-Run-2.ipynb
jupyter nbconvert --to pdf U-Run-2.ipynb
我有一个简单的 jupyter notebook 文件 (Untitled.ipynb
),它开始于:
{
"cells": [
{
"cell_type": "code",
"execution_count": 1,
"metadata": {},
我想 运行 它并将 运行ned 版本转换为 PDF。第 1 步,运行:
jupyter run Untitled.ipynb > U-Run.ipynb
这让我得到了一些在任何地方都使用单引号的东西,jupyter nbconvert
barfs on:
$ head U-Run.ipynb
{'cells': [{'cell_type': 'code',
'execution_count': 1,
'metadata': {},
'outputs': [{'name': 'stdout', 'output_type': 'stream', 'text': ['3\n']}],
当我尝试转换为 PDF 时:
$ jupyter nbconvert --to pdf U-Run.ipynb
[NbConvertApp] Converting notebook U-Run.ipynb to pdf
Traceback (most recent call last):
File "/usr/lib/python3/dist-packages/nbformat/reader.py", line 14, in parse_json
nb_dict = json.loads(s, **kwargs)
File "/usr/lib/python3.8/json/__init__.py", line 357, in loads
return _default_decoder.decode(s)
File "/usr/lib/python3.8/json/decoder.py", line 337, in decode
obj, end = self.raw_decode(s, idx=_w(s, 0).end())
File "/usr/lib/python3.8/json/decoder.py", line 353, in raw_decode
obj, end = self.scan_once(s, idx)
json.decoder.JSONDecodeError: Expecting property name enclosed in double quotes: line 1 column 2 (char 1)
然后回溯结束:
nbformat.reader.NotJSONError: Notebook does not appear to be JSON: '{\'cells\': [{\'cell_type\': \'code\',\...
如果我从一个普通的 python 会话中导入 json
并尝试一个简单的测试,我会得到双引号:
>>> json.dumps(dict(a=1))
'{"a": 1}'
所以我不明白为什么 jupyter run
输出单引号 "json".
所有东西的版本是:
$ jupyter --version
jupyter core : 4.6.3
jupyter-notebook : 6.3.0
qtconsole : 5.0.3
ipython : 7.29.0
ipykernel : 6.5.0
jupyter client : 6.1.12
jupyter lab : 3.0.0
nbconvert : 6.0.7
ipywidgets : 7.6.3
nbformat : 5.0.4
traitlets : 5.1.1
Python: 3.8.10
Ubuntu 20.04
Apparently,也可以告诉nbconvert
到--execute
:
jupyter nbconvert --to pdf --execute U-Run.ipynb
您还可以使用papermill
执行ipynb文件,然后将输出转换为PDF:
papermill U-Run.ipynb U-Run-2.ipynb
jupyter nbconvert --to pdf U-Run-2.ipynb