You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何调整Jupyter Notebook输出框宽度?已尝试部分方案无效

如何调整Jupyter Notebook输出框的宽度?

我想调整Jupyter Notebook输出框的宽度,当前输出框显示受限,无法完整展示DataFrame内容(如图所示):

Jupyter Notebook输出框宽度受限

以下是我的示例代码:

import pandas as pd

# 创建包含数据的DataFrame
data = {
    'eigen_of_inhuur': ['', '', '', '', '', '', '', '', '', '', '', '', '', '', ''],
    'Basis': ['Inzet vrachtauto', 'horizontaa/verticaal transport', 'horizontaal/verticaal transport',
                        'horizontaal/verticaal transport op perron', 'huur trilplaat, 5,0 ton, voor- en achteruit',
                        'huur wacker stamper', 'huur hydraulische sloophamer 2-4 ton, huur per...',
                        'huur mini- kraan 1.5 ton', 'huur mini- kraan 3.5 ton', 'huur mobiele kraan 14 ton',
                        'huur rups kraan 80 ton, Sennebogen', 'huur shovel 15 ton. 3000 ltr.',
                        'huur trilwals 7-9 ton, inkl. machinist, brandstof', 'inzet shovel', 'veeg/zuigauto klein Ravo'],
    'hoeveelheid': [47.45, 1, 96, 304.65, 315.04, 226.48, 11.2, 265.74, 420.81, 654.71, 56, 33.82, 2.88, 286, 1.34],
    'eenheid': ['uur', 'uur', 'uur', 'uur', 'uur', 'uur', 'uur', 'uur', 'uur', 'uur', 'uur', 'uur', 'uur', 'uur', 'uur']
}

df = pd.DataFrame(data)

# 设置显示最大列数
pd.set_option('display.max_columns', None)

# 打印DataFrame
print(df)

我已经尝试过用HTML和CSS代码加宽输出框,也试过切换到JupyterLab,但这些方法都没效果。请问该怎么解决这个问题?

提前感谢!


解决方法

1. 调整Pandas显示参数(针对DataFrame)

除了设置display.max_columns,还需补充两个关键参数来适配内容宽度:

# 让列宽自适应内容(-1表示不限制列宽)
pd.set_option('display.max_colwidth', -1)
# 设置输出的整体字符宽度,数值可根据需求调整
pd.set_option('display.width', 1000)

设置完成后,直接输入df并运行(不要用print(df)),Jupyter会以更适配的格式渲染表格。

2. 临时修改当前Notebook的CSS样式

在Notebook中新建单元格,运行以下代码,直接调整输出区域宽度:

from IPython.display import display, HTML
display(HTML("""
<style>
div.output_area {
    max-width: 95% !important;
    width: 95% !important;
}
div.cell {
    max-width: 95% !important;
}
div.input_area {
    max-width: 95% !important;
}
</style>
"""))

运行后当前Notebook的输出框会立即扩展到浏览器95%的宽度,无需重启。

3. 配置永久生效的宽屏样式

如果希望所有新打开的Notebook都默认使用宽屏,可按以下步骤操作:

  1. 打开终端,执行jupyter notebook --generate-config生成配置文件
  2. 在生成的jupyter_notebook_config.py中添加:
c.NotebookApp.tornado_settings = {
    'headers': {
        'Content-Security-Policy': "default-src 'self' 'unsafe-inline' 'unsafe-eval'"
    }
}
  1. 在用户目录下创建~/.jupyter/custom/custom.css文件,写入:
div.output_area {
    max-width: 95% !important;
    width: 95% !important;
}
div.cell {
    max-width: 95% !important;
}
div.input_area {
    max-width: 95% !important;
}

重启Jupyter Notebook后,所有新Notebook都会自动应用该宽屏样式。

4. JupyterLab的调整方案

若使用JupyterLab,可右键点击输出区域,选择Enable Scrolling for Outputs关闭滚动限制;也可以进入设置界面,找到Notebook: Cell Width选项,将其设置为'100%'。


内容的提问来源于stack exchange,提问作者Remco

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.25 17:13:33