164

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

Reblochon Masque
  • 35,405
  • 10
  • 55
  • 80
mbh86
  • 6,078
  • 3
  • 18
  • 31
  • 30
    It's not used very often, but in fact there is a config option that should do this - set `InteractiveShell.ast_node_interactivity` to `'all'` in the [IPython kernel config file](http://ipython.readthedocs.org/en/stable/config/options/kernel.html). – Thomas K Apr 23 '16 at 14:25
  • While it doesn't meet the "print all the interactive output"-part of the OP description, I think the title might draw users here where use of [JupyterLab's debugger](https://jupyterlab.readthedocs.io/en/stable/user/debugger.html) might suffice. They can toggle on a stop on the first line of a cell and then step through each next line by pressing 'Next' icon in the 'CALLSTACK' pane to see the values of the variables change. – Wayne Dec 08 '22 at 19:35

4 Answers4

269

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

from IPython.core.interactiveshell import InteractiveShell
InteractiveShell.ast_node_interactivity = "all"
mbh86
  • 6,078
  • 3
  • 18
  • 31
  • 2
    This is a great tip. - What is the flag value to revert to the last line of output only? – Reblochon Masque Apr 25 '16 at 08:55
  • 23
    The default value is: 'last_expr'. You can find a lot of other options here: http://ipython.readthedocs.org/en/stable/config/options/terminal.html – mbh86 Apr 25 '16 at 09:15
  • 11
    For reference, the options for that are 'all', 'none', 'last' and 'last_expr'. The difference between 'last' and 'last_expr': if your cell ends with, say, a loop containing an expression, 'last' will show you the results from that expression in each iteration of the loop. 'last_expr' (the default) won't show that: it will only display the result of a bare expression at the end of the cell. – Thomas K Apr 25 '16 at 11:46
  • 1
    Holy moly...this feature is a killer. – flow2k Aug 02 '19 at 23:59
  • 6
    The new(ish) `last_expr_or_assign` is amazing for doing demos! No more retyping the same item multiple times just to get it to print too. – Henry Schreiner Jul 13 '20 at 15:59
  • this only seems to work if put in a separate cell – philb Jan 24 '23 at 16:04
40

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"
jrtapsell
  • 6,719
  • 1
  • 26
  • 49
William
  • 1,201
  • 10
  • 3
17

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

Ani Aggarwal
  • 411
  • 5
  • 8
  • 1
    Is there a way to do it per cell? say I just want to see the full result of a Pandas.Series on a couple of cells, is it possible? – Ricardo Sanchez Jan 21 '21 at 08:08
  • 1
    @RicardoSanchez maybe just use the `display()` function? First import it `from IPython.display import display` then just do `display(df)`. – Ani Aggarwal Jan 31 '21 at 15:47
4

we can add display method before every statement that needs to be displayed

enter image description here