8

I am working on this python notebook in Google Collab: https://github.com/AllenDowney/ModSimPy/blob/master/notebooks/chap01.ipynb

I had to change the configuration line because the one stated in the original was erroring out:

# Configure Jupyter to display the assigned value after an assignment

# Line commented below because errors out
# %config InteractiveShell.ast_node_interactivity='last_expr_or_assign'

# Edit solution given below
%config InteractiveShell.ast_node_interactivity='last_expr'

However, I think the original statement was meant to show values of assignments (if I'm not mistaken), so that when I run the following cell in the notebook, I should see an output:

meter = UNITS.meter
second = UNITS.second
a = 9.8 * meter / second**2

If so how can I make the notebook on google collab show output of assignments?

Angel Cloudwalker
  • 2,015
  • 5
  • 32
  • 54

5 Answers5

3

The short answer is: you can not show output of assignments in Colab.

Your confusion comes from how Google Colab works. The original script is meant to run in IPython. But Colab is not a regular IPython. When you run IPython shell, your %config InteractiveShell.ast_node_interactivity options are (citing documentation)

‘all’, ‘last’, ‘last_expr’ , ‘last_expr_or_assign’ or ‘none’, specifying which nodes should be run interactively (displaying output from expressions). ‘last_expr’ will run the last node interactively only if it is an expression (i.e. expressions in loops or other blocks are not displayed) ‘last_expr_or_assign’ will run the last expression or the last assignment. Other values for this parameter will raise a ValueError.

all will display all the variables, but not the assignments, for example

x = 5
x
y = 7
y

Out[]:
5
7

The differences between the options become more significant when you want to display variables in the loop.

In Colab your options are restricted to ['all', 'last', 'last_expr', 'none']. If you select all, the result for the above cell will be

Out[]:
57

Summarizing all that, there is no way of showing the result of assignment in Colab. Your only option (AFAIK) is to add the variable you want to see to the cell where it is assigned (which is similar to regular print):

meter = UNITS.meter
second = UNITS.second
a = 9.8 * meter / second**2
a
igrinis
  • 12,398
  • 20
  • 45
2

Google Colab has not yet been upgraded to the latest IPython version- if you explicitly upgrade with

!pip install -U ipython 

then last_expr_or_assign will work.

Angel Cloudwalker
  • 2,015
  • 5
  • 32
  • 54
0

Well the easy way is to just wrap your values in print statements like:

print(meter)
print(second)
print(a)

However, if you want to do it the jupyter way, looks like the answer is

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

Found the above from this link: https://stackoverflow.com/a/36835741/7086982

anash
  • 86
  • 1
  • 9
0

As mentioned by others, it doesn't work as last_expr_or_assign was introduced in juptyer v6.1, and colab is using v5.x. Upgrading the jupyter version on colab might cause some instability (warning shown by colab):

WARNING: Upgrading ipython, ipykernel, tornado, prompt-toolkit or pyzmq can
cause your runtime to repeatedly crash or behave in unexpected ways and is not
recommended

One other solution is to use an extension such as ipydex, which provides magic comments (like ##:, ##:T, ##:S) which cause that either the return value or the right hand side of an assignment of a line is displayed.

!pip install ipydex
%load_ext ipydex.displaytools

e.g.

a = 4
c = 5 ##:

output

c := 5

---
kHarshit
  • 11,362
  • 10
  • 52
  • 71
0

Updating iPython in a colab environment may not be desirable:

At the time of this writing (July 2023), upgrading iPython for markdown variable support is possible but introduces possible version incompatibilities with the current version of google-colab (1.0.0)

! pip install -U ipython

ERROR: pip's dependency resolver does not currently take into account all the packages that are installed. This behaviour is the source of the following dependency conflicts. google-colab 1.0.0 requires ipython==7.34.0, but you have ipython 8.14.0 which is incompatible ...

You must restart the runtime in order to use newly installed versions.

If you favor stability, you could instead use the included IPython library to generate markdown from heredoc templates:

from IPython.display import display, Markdown

name = "Foo"
age = 50

markdown_text = f'''
# Heading

Name: {name}
Age: {age}
'''

display(Markdown(markdown_text))
random-forest-cat
  • 33,652
  • 11
  • 120
  • 99