You do not need to check if the code is being executed from the laptop; display() prints text when called from the command line.
test.py :
from IPython.display import display import pandas as pd my_df = pd.DataFrame({'foo':[1,2,3],'bar':[7,8,9]}) display(my_df)
From the command line:
$ python test.py bar foo 0 7 1 1 8 2 2 9 3
From a Jupyter laptop:

UPDATE
To check if you are working inside the Ipython interactive shell (command line or browser-based), check get_ipython . (Adapted from Ipython docs)
Changed test.py :
from IPython.display import display, HTML import pandas as pd my_df = pd.DataFrame({'foo':[1,2,3],'bar':[7,8,9]}) try: get_ipython display(my_df) except: print(my_df)
This approach would be:
- beautiful print in Jupyter browser
- print text at startup as a script from the command line (for example, python test.py )
- if you run line by line in the Python shell, it will not turn into an interactive Ipython shell after printing
andrew_reece
source share