chinex
chinex

Reputation: 45

Is it possible to switch back and forth between displaying multiple outputs and single output in a Jupyter Notebook cell?

To display multiple outputs in a cell I use,

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

But what if for I have a cell that I only want a single output from, how would I switch back in the same notebook.

I tried issuing

    InteractiveShell.ast_node_interactivity = "last_expr"

in the cell where I wanted a single output from but it didn't seem to work.

Upvotes: 2

Views: 738

Answers (1)

Matt
Matt

Reputation: 27833

You can issue the following in an IPython (terminal, notebook, ...) prompt:

%config

It will tell you which object can be configured:

Available objects for config:
 AliasManager
 DisplayFormatter
 HistoryManager
 IPCompleter
 IPKernelApp
 InlineBackend
 LoggingMagics
 MagicsManager
 PrefilterManager
 ScriptMagics
 StoreMagics
 ZMQInteractiveShell

Note that ZMQInteractiveShell will be TerminalInteractiveShell in a terminal. In our case that is, what we are interested in configuring, let's ask config for more info:

%config ZMQInteractiveShell

ZMQInteractiveShell options
-------------------------
ZMQInteractiveShell.ast_node_interactivity=<Enum>
    Current: 'last_expr'
    Choices: ['all', 'last', 'last_expr', 'none', 'last_expr_or_assign']
    'all', 'last', 'last_expr' or 'none', 'last_expr_or_assign' specifying which
    nodes should be run interactively (displaying output from expressions).
ZMQInteractiveShell.ast_transformers=<List>
    Current: []

Oh ! Well then let's assign that:

%config InteractiveShell.ast_node_interactivity="last_expr"

And you are set to go, and moreover you've learn how to dive into the IPython configuration system.

Note that it may not work for all config options (if so open a bug report) but it should for last_node_interactivity, and the parser is a bit picky, you may need to not have spaces around the =.

Enjoy !

Upvotes: 3

Related Questions