Using Python (version: 3.10.6) and the Rich (version: 13.3.1) package, I'm attempting to display two tables side by side inside a panel, like this:
from rich.panel import Panel
from rich.table import Table
from rich.console import Console
console = Console()
table1 = Table()
table1.add_column("a")
table1.add_column("b")
table1.add_row("a", "b")
table1.add_row("a", "b")
table2 = Table()
table2.add_column("c")
table2.add_column("d")
table2.add_row("c", "d")
table2.add_row("c", "d")
panel = Panel.fit(
[table1, table2],
title="My Panel",
border_style="red",
title_align="left",
padding=(1, 2),
)
console.print(panel)
This code is producing the following Traceback:
File "/home/linux/ToolkitExtraction (copy 1)/table_in_panel.py", line 33, in <module>
console.print(panel)
File "/home/linux/.local/lib/python3.10/site-packages/rich/console.py", line 1694, in print
extend(render(renderable, render_options))
File "/home/linux/.local/lib/python3.10/site-packages/rich/console.py", line 1326, in render
for render_output in iter_render:
File "/home/linux/.local/lib/python3.10/site-packages/rich/panel.py", line 204, in __rich_console__
else console.measure(
File "/home/linux/.local/lib/python3.10/site-packages/rich/console.py", line 1278, in measure
measurement = Measurement.get(self, options or self.options, renderable)
File "/home/linux/.local/lib/python3.10/site-packages/rich/measure.py", line 109, in get
get_console_width(console, options)
File "/home/linux/.local/lib/python3.10/site-packages/rich/padding.py", line 132, in __rich_measure__
measure_min, measure_max = Measurement.get(console, options, self.renderable)
File "/home/linux/.local/lib/python3.10/site-packages/rich/measure.py", line 119, in get
raise errors.NotRenderableError(
rich.errors.NotRenderableError: Unable to get render width for [<rich.table.Table object at 0x7f562c1c2470>, <rich.table.Table object at 0x7f562c0411e0>]; a str, Segment, or object with __rich_console__ method is required
I tried inserting console.width = 150
prior to panel creation though this did not make any difference.
I think if you want side-by-side tables inside your panel, you'll need to wrap them in a Columns
:
panel = Panel.fit(
Columns([table1, table2]),
title="My Panel",
border_style="red",
title_align="left",
padding=(1, 2),
)
console.print(panel)
That results in:
╭─ My Panel ───────────────────────────────────────────────────────────────────╮
│ │
│ ┏━━━┳━━━┓ ┏━━━┳━━━┓ │
│ ┃ a ┃ b ┃ ┃ c ┃ d ┃ │
│ ┡━━━╇━━━┩ ┡━━━╇━━━┩ │
│ │ a │ b │ │ c │ d │ │
│ │ a │ b │ │ c │ d │ │
│ └───┴───┘ └───┴───┘ │
│ │
╰──────────────────────────────────────────────────────────────────────────────╯
You'll note that the panel is taking up the full terminal width despite the fact that we're calling Panel.fit
. I haven't found a solution to that issue, but it is possible to provide Panel
with an explicit width:
panel = Panel.fit(
Columns([table1, table2]),
width=40,
title="My Panel",
border_style="red",
title_align="left",
padding=(1, 2),
)
Which produces:
╭─ My Panel ───────────────────────────╮
│ │
│ ┏━━━┳━━━┓ ┏━━━┳━━━┓ │
│ ┃ a ┃ b ┃ ┃ c ┃ d ┃ │
│ ┡━━━╇━━━┩ ┡━━━╇━━━┩ │
│ │ a │ b │ │ c │ d │ │
│ │ a │ b │ │ c │ d │ │
│ └───┴───┘ └───┴───┘ │
│ │
╰──────────────────────────────────────╯