question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Coalesce multiple stdout/stderr Notebook outputs into single blocks

See original GitHub issue

I am struggling to understand why the cells output is split in multiple lines when the book is rendered using HTML. My stata.ipynb notebook source code looks like

 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Operators\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "\n",
      "(1978 Automobile Data)\n",
      "\n",
      "\n",
      "      Source |       SS           df       MS      Number of obs   =        74\n",
      "-------------+----------------------------------   F(1, 72)        =     20.26\n",
      "       Model |   139449474         1   139449474   Prob > F        =    0.0000\n",
      "    Residual |   495615923        72  6883554.48   R-squared       =    0.2196\n",
      "-------------+----------------------------------   Adj R-squared   =    0.2087\n",
      "       Total |   635065396        73  8699525.97   Root MSE        =    2623.7\n",
      "\n",
      "------------------------------------------------------------------------------\n",
      "       price |      Coef.   Std. Err.      t    P>|t|     [95% Conf. Interval]\n",
      "-------------+----------------------------------------------------------------\n",
      "         mpg |  -238.8943   53.07669    -4.50   0.000    -344.7008   -133.0879\n",
      "       _cons |   11253.06   1170.813     9.61   0.000     8919.088    13587.03\n",
      "------------------------------------------------------------------------------\n"
     ]
    }
   ],
   "source": [
    "sysuse auto\n",
    "reg price mpg"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Stata",
   "language": "stata",
   "name": "stata"
  },
  "language_info": {
   "codemirror_mode": "stata",
   "file_extension": ".do",
   "mimetype": "text/x-stata",
   "name": "stata",
   "version": "15.1"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 4
}

After I built the book using

(base) Federicos-MacBook-Pro:books federico$ jupyter-book clean mybook/ --all

===============================================================================

Your _build directory has been removed

===============================================================================

(base) Federicos-MacBook-Pro:books federico$ jupyter-book build mybook/
Running Jupyter-Book v0.8.1
Source Folder: /Users/federico/Desktop/Dropbox/Teaching/jupyter/books/mybook
Config Path: /Users/federico/Desktop/Dropbox/Teaching/jupyter/books/mybook/_config.yml
Output Path: /Users/federico/Desktop/Dropbox/Teaching/jupyter/books/mybook/_build/html
Running Sphinx v2.4.0
making output directory... done
myst v0.12.9: MdParserConfig(renderer='sphinx', commonmark_only=False, dmath_enable=True, dmath_allow_labels=True, dmath_allow_space=True, dmath_allow_digits=True, amsmath_enable=False, deflist_enable=False, update_mathjax=True, admonition_enable=False, figure_enable=False, disable_syntax=[], html_img_enable=False, url_schemes=['mailto', 'http', 'https'], heading_anchors=None)
building [mo]: targets for 0 po files that are out of date
building [html]: targets for 1 source files that are out of date
updating environment: [new config] 1 added, 0 changed, 0 removed
Executing: stata in: /Users/federico/Desktop/Dropbox/Teaching/jupyter/books/mybook                                                           

looking for now-outdated files... none found
pickling environment... done
checking consistency... done
preparing documents... done
writing output... [100%] stata                                                                                                               
generating indices...  genindexdone
writing additional pages...  searchdone
copying static files... ... done
copying extra files... done
dumping search index in English (code: en)... done
dumping object inventory... done
build succeeded.

The HTML pages are in mybook/_build/html.

===============================================================================

Finished generating HTML for book.
Your book's HTML pages are here:
    mybook/_build/html/
You can look at your book by opening this file in a browser:
    mybook/_build/html/index.html
Or paste this line directly into your browser bar:
    file:///Users/federico/Desktop/Dropbox/Teaching/jupyter/books/mybook/_build/html/index.html            

===============================================================================

the notebook source code in the mybook/_build/jupyter_execute appears to be different and, as you can see below, the output of each “code” cell is split in separate lines

{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Operators\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "\n",
      "(1978 Automobile Data)\n"
     ]
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "\n",
      "\n",
      "      Source |       SS           df       MS      Number of obs   =        74\n"
     ]
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "-------------+----------------------------------   F(1, 72)        =     20.26\n"
     ]
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "       Model |   139449474         1   139449474   Prob > F        =    0.0000\n"
     ]
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "    Residual |   495615923        72  6883554.48   R-squared       =    0.2196\n"
     ]
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "-------------+----------------------------------   Adj R-squared   =    0.2087\n"
     ]
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "       Total |   635065396        73  8699525.97   Root MSE        =    2623.7\n"
     ]
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "\n",
      "------------------------------------------------------------------------------\n"
     ]
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "       price |      Coef.   Std. Err.      t    P>|t|     [95% Conf. Interval]\n"
     ]
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "-------------+----------------------------------------------------------------\n"
     ]
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "         mpg |  -238.8943   53.07669    -4.50   0.000    -344.7008   -133.0879\n"
     ]
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "       _cons |   11253.06   1170.813     9.61   0.000     8919.088    13587.03\n"
     ]
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "------------------------------------------------------------------------------\n"
     ]
    }
   ],
   "source": [
    "sysuse auto\n",
    "reg price mpg"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Stata",
   "language": "stata",
   "name": "stata"
  },
  "language_info": {
   "codemirror_mode": "stata",
   "file_extension": ".do",
   "mimetype": "text/x-stata",
   "name": "stata",
   "version": "15.1"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 4
}

This is how the HTML final output looks like

Screen Shot 2020-09-17 at 11 25 00

Do you have any idea of why this is happening?

Many thanks! Federico

Environment

  • Python v3.7.6
  • Jupyter-Book v0.8.1
  • Sphinx v2.4.0
  • myst v0.12.9
  • Operating System: OsX Catalina

Issue Analytics

  • State:closed
  • Created 3 years ago
  • Comments:9 (4 by maintainers)

github_iconTop GitHub Comments

1reaction
chrisjsewellcommented, Oct 3, 2021

This is now available in https://github.com/executablebooks/MyST-NB/compare/v0.13.0...v0.13.1 (as nb_merge_streams=True) 😄 and so will be incorporated into jupyter-book in the next release

1reaction
fbelotticommented, Sep 17, 2020

For those who have the same issue, a “temporary” workaround is to execute the notebooks on your own, copy and paste all the .ipynb files in the ./_build/jupyter_execute/ folder and put the execute_notebooks: off setting in the execute section of the book’s _config.yml file.

execute:
  execute_notebooks: off 

Hope this helps.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Suppress stdout / stderr print from Python functions
As of python 3.5 we can do this with minimal work using built-ins in contextlib , namely redirect_stdout and redirect_stderr .
Read more >
How To Block Stderr Output In Jupyter Notebook - ADocLib
Coalesce multiple stdout /stderr Notebook outputs into single blocks. I am struggling to understand why the cells output is split in multiple lines....
Read more >
Capturing Output With %%capture - | notebook.community
IPython has a cell magic, %%capture , which captures the stdout/stderr of a cell. With this magic you can discard these streams or...
Read more >
Built-in magic commands — IPython 8.7.0 documentation
This magic command support two ways of activating debugger. One is to activate debugger before executing code. This way, you can set a...
Read more >
Three ways to blog with code · Markov Wanderer
I'll look at three ways to do this: Jupyter Notebooks, Codebraid, ... .cb.nb} show=code+stdout:raw+stderr , shows the code, the raw output, ...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found