Skip to content

Bad rendering of stream outputs when executing through nbconvert #132

@knuesel

Description

@knuesel

When a notebook is executed by nbconvert, some stream outputs are split into many small outputs, causing bad rendering.

For example the notebook test.ipynb contains the following cells:

help
a = 1;
whos

The help command is included here as an example where the stream output works correctly. The problem is with the second cell. When executed in Jupyterlab it renders correctly:

Variables visible from the current scope:

variables in scope: top scope

  Attr   Name        Size                     Bytes  Class
  ====   ====        ====                     =====  ===== 
         a           1x1                          8  double

Total is 1 element using 8 bytes

and the output is stored correctly in the ipynb JSON:

   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Variables visible from the current scope:\n",
      "\n",
      "variables in scope: top scope\n",
      "\n",
      "  Attr   Name        Size                     Bytes  Class\n",
      "  ====   ====        ====                     =====  ===== \n",
      "         a           1x1                          8  double\n",
      "\n",
      "Total is 1 element using 8 bytes\n",
      "\n"
     ]
    }
   ],

and calling nbconvert --to html test.ipynb gives a good-looking HTML file: test.html. Screenshot:

Image

However when calling jupyter-nbconvert --to html test.ipynb --execute to execute the notebook while converting, the text output is badly formatted: test.html. Screenshot:

Image

Running jupyter-nbconvert --to notebook test.ipynb --execute to see the ipynb JSON for the execution result shows that the stream output is stored as a large number of small outputs (almost one output per word or blank):

JSON of stream output for "whos" command
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Variables visible from the current scope:\n"
     ]
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "\n",
      "variables in scope: "
     ]
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "top scope"
     ]
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "\n",
      "\n"
     ]
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "  "
     ]
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Attr  "
     ]
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      " "
     ]
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Name  "
     ]
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      " "
     ]
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "     "
     ]
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Size"
     ]
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "            "
     ]
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "  "
     ]
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "       Bytes"
     ]
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "  "
     ]
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Class"
     ]
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "\n"
     ]
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "  ====   ====        ====                     =====  ===== "
     ]
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "\n"
     ]
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "  "
     ]
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "      "
     ]
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      " "
     ]
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "a     "
     ]
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      " "
     ]
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "     "
     ]
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "1x1"
     ]
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "             "
     ]
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "  "
     ]
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "           8"
     ]
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "  "
     ]
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "double"
     ]
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "\n"
     ]
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "\n",
      "Total is "
     ]
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "1"
     ]
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      " element"
     ]
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      " using "
     ]
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "8"
     ]
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      " bytes"
     ]
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "\n"
     ]
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "\n"
     ]
    }
   ],
   "source": [
    "a = 1;\n",
    "whos"
   ]
  }
 ],

Full JSON file: test.nbconvert.ipynb

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions