简体   繁体   中英

jupyter run produces invalid JSON with single quotes

I have a simple jupyter notebook file ( Untitled.ipynb ) that starts:

{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},

and I want to run it and convert the runned version to PDF. Step 1, run:

jupyter run Untitled.ipynb > U-Run.ipynb

and that gets me something that uses single quotes everywhere which 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']}],

and when I try and convert to 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)

then a traceback that ends:

nbformat.reader.NotJSONError: Notebook does not appear to be JSON: '{\'cells\': [{\'cell_type\': \'code\',\...

If I import json from a plain python session and try a simple test I get double quotes:

>>> json.dumps(dict(a=1))
'{"a": 1}'

so I don't understand why jupyter run is outputting single-quoted "json".

Versions of all the things are:

$ 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 , you can also tell nbconvert to --execute :

jupyter nbconvert --to pdf --execute U-Run.ipynb 

You can also use papermill to execute an ipynb file, then convert the output to PDF:

papermill U-Run.ipynb U-Run-2.ipynb 
jupyter nbconvert --to pdf U-Run-2.ipynb 

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM