Skip to content

Instantly share code, notes, and snippets.

@sashadev-sky
Last active December 10, 2020 07:06
Show Gist options
  • Save sashadev-sky/a2614ac2584b0249d42122419a2ba01a to your computer and use it in GitHub Desktop.
Save sashadev-sky/a2614ac2584b0249d42122419a2ba01a to your computer and use it in GitHub Desktop.
Pretty Print All Cell’s Outputs (and not just the last output of the cell)
Display the source blob
Display the rendered blob
Raw
/* normally only the last_exp in a code cell is printed to output for us.
we can modify this setting by updating from the default value `last_expr` to 'all'
for the function below */
from IPython.core.interactiveshell import InteractiveShell
InteractiveShell.ast_node_interactivity = "all"
/* note: you have to run the setting change in a separate cell for it to take effect for the next cell run.
To make this behavior consistent across all notebooks edit: `~/.ipython/profile_default/ipython_config.py`: */
c = get_config()
c.InteractiveShell.ast_node_interactivity = "all"
/* be careful - this has side effects - would not recommend setting globally ever.
Some libraries will start spewing a lot of noise (e.g. matplotlib). To fix that:
a) add `;` at the end of the lines whose output you don’t want to be displayed.
` b) `;` doesn't work when the code line that outputs something is part of some loop,
in which case the workaround of assigning to `_` does the trick */
/* a */
fig, axes = plt.subplots(2, 1, figsize=(20, 20))
axes[0].set_ylim(-5, 5);
/* b */
fig, axes = plt.subplots(2, 1, figsize=(20, 20))
for i in [1,2]:
_ = axes[i].set_ylim(-5, 5)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment