How to display full output in Jupyter, not only last result?

PythonIpythonJupyter

Python Problem Overview


I want Jupyter to print all the interactive output without resorting to print, not only the last result. How to do it?

Example :

a=3
a
a+1

I would like to display

> 3
> 4

Python Solutions


Solution 1 - Python

Thanks to Thomas, here is the solution I was looking for:

from IPython.core.interactiveshell import InteractiveShell
InteractiveShell.ast_node_interactivity = "all"

Solution 2 - Python

https://www.dataquest.io/blog/jupyter-notebook-tips-tricks-shortcuts/

  1. Place this code in a Jupyter cell:

    from IPython.core.interactiveshell import InteractiveShell

    InteractiveShell.ast_node_interactivity = "all"

  2. In Windows, the steps below makes the change permanent. Should work for other operating systems. You might have to change the path.

    C:\Users\your_profile\.ipython\profile_default

Make a ipython_config.py file in the profile_defaults with the following code:

c = get_config()

c.InteractiveShell.ast_node_interactivity = "all"

Solution 3 - Python

Per Notebook Basis

As others have answered, putting the following code in a Jupyter Lab or Jupyter Notebook cell will work:

from IPython.core.interactiveshell import InteractiveShell

InteractiveShell.ast_node_interactivity = "all"

Permanent Change

However, if you would like to make this permanent and use Jupyter Lab, you will need to create an IPython notebook config file. Run the following command to do so (DO NOT run if you use Jupyter Notebook - more details below):

ipython profile create

If you are using Jupyter Notebook, this file should have already been created and there will be no need to run it again. In fact, running this command may overwrite your current preferences.

Once you have this file created, for Jupyter Lab and Notebook users alike, add the following code to the file C:\Users\USERNAME\.ipython\profile_default\ipython_config.py:

c.InteractiveShell.ast_node_interactivity = "all"

I found there is no need for c = get_config() in the newer versions of Jupyter, but if this doesn't work for you, add the c = get_config() to the beginning of the file.

For more flag options other than "all", visit this link: https://ipython.readthedocs.io/en/stable/config/options/terminal.html#configtrait-InteractiveShell.ast_node_interactivity

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
Questionmbh86View Question on Stackoverflow
Solution 1 - Pythonmbh86View Answer on Stackoverflow
Solution 2 - PythonWilliamView Answer on Stackoverflow
Solution 3 - PythonAni AggarwalView Answer on Stackoverflow