繁体   English   中英

ipython:如何设置终端宽度

[英]ipython: how to set terminal width

当我使用ipython terminal并想要打印一个包含多列的numpy.ndarray ,这些行会自动在 80 个字符左右的某个地方断开(即行的宽度为 cca 80 个字符):

z = zeros((2,20))
print z

据推测,ipython 预计我的终端有 80 列。 然而,事实上,我的终端有 176 个字符的宽度,我想使用全宽。

我曾尝试更改以下参数,但这没有效果:

c.PlainTextFormatter.max_width = 160

如何告诉ipython使用我终端的全宽?

我在 Debian Wheezy 上使用ipython 1.2.1

你可以看到你当前的线宽

numpy.get_printoptions()['linewidth']

并将其设置为

numpy.set_printoptions(linewidth=160)

自动设置打印宽度

如果您希望自动设置终端宽度,您可以让 Python 执行启动脚本。 因此,创建一个文件~/.python_startup.py或任何您想调用的文件,在其中包含以下内容:

# Set the printing width to the current terminal width for NumPy.
#
# Note: if you change the terminal's width after starting Python,
#       it will not update the printing width.
from os import getenv
terminal_width = getenv('COLUMNS')
try:
    terminal_width = int(terminal_width)
except (ValueError, TypeError):
    print('Sorry, I was unable to read your COLUMNS environment variable')
    terminal_width = None

if terminal_width is not None and terminal_width > 0:
    from numpy import set_printoptions
    set_printoptions(linewidth = terminal_width)

del terminal_width

并让 Python 每次都执行这个,打开你的~/.bashrc文件,并添加

# Instruct Python to execute a start up script
export PYTHONSTARTUP=$HOME/.python_startup.py
# Ensure that the startup script will be able to access COLUMNS
export COLUMNS

在对代码进行一些挖掘之后,您正在寻找的变量似乎是numpy.core.arrayprint._line_width ,默认情况下为 75。 将其设置为 160 对我有用:

>>> numpy.zeros((2, 20))
array([[ 0.,  0.,  0.,  0.,  0.,  0.,  0.,  0.,  0.,  0.,  0.,  0.,  0.,  0.,  0.,  0., 0.,  0.,  0.,  0.],
       [ 0.,  0.,  0.,  0.,  0.,  0.,  0.,  0.,  0.,  0.,  0.,  0.,  0.,  0.,  0.,  0.,  0.,  0.,  0.,  0.]])

默认用于数组格式化的函数是numpy.core.numeric.array_repr ,尽管您可以使用numpy.core.numeric.set_string_function更改它。

要在窗口大小更改时自动调整 numpy 和 IPython 的大小,请将以下内容添加到ipython_config.py

import IPython
import signal
import shutil
import sys
try:
    import numpy as np
except ImportError:
    pass

c = get_config()

def update_terminal_width(*ignored):
    """Resize the IPython and numpy printing width to match the terminal."""
    w, h = shutil.get_terminal_size()

    config = IPython.get_ipython().config
    config.PlainTextFormatter.max_width = w - 1
    shell = IPython.core.interactiveshell.InteractiveShell.instance()
    shell.init_display_formatter()

    if 'numpy' in sys.modules:
        import numpy as np
        np.set_printoptions(linewidth=w - 5)

# We need to configure IPython here differently because get_ipython() does not
# yet exist.
w, h = shutil.get_terminal_size()
c.PlainTextFormatter.max_width = w - 1
if 'numpy' in sys.modules:
    import numpy as np
    np.set_printoptions(linewidth=w - 5)

signal.signal(signal.SIGWINCH, update_terminal_width)

如果您想将 numpy 的加载延迟到必要时,请查看Python 3中的Post import hooks 以获取解决方案。

如果您不使用 IPython,请将上述内容放在您的 PYTHONSTARTUP 文件中并删除 IPython 特定的行。

使用这种方法:

np.set_printoptions(linewidth=300)

并使用此扩展您的 iphyton 视图:

from IPython.core.display import display, HTML
display(HTML("<style>.container { width:95% !important; }</style>"))

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM