【发布时间】:2017-10-21 09:07:21
【问题描述】:
我正在创建一个 python 模块,该模块具有显示 pandas DataFrame (my_df) 的功能。
如果用户将模块导入到 Jupyter 笔记本中,我希望通过以下方式为 DataFrame 提供“漂亮”的格式:
from IPython.display import display, HTML
display(my_df)
如果用户不在 Jupyter 笔记本中,我想显示 DataFrame 的文本形式:
print(my_df)
如何检查代码是否从 Jupyter 笔记本运行?或者,如何从命令行以文本形式显示 DataFrame,而不是在将 HTML 表单导入 Jupyter 笔记本时显示它?
from IPython.display import display, HTML
def my_func(my_df):
if [... code to check for Jupyter notebook here ...]:
display(my_df)
else:
print(my_df)
【问题讨论】:
标签: python pandas jupyter-notebook jupyter