Code Monkey home page Code Monkey logo

Comments (11)

willmcgugan avatar willmcgugan commented on May 21, 2024

What trouble are you having? Rich should detect the width of the terminal automatically. Post a screenshot if you like.

from rich.

bwalrond avatar bwalrond commented on May 21, 2024

The table doesn't seem to expand beyond 80chars, instead it squeezes to fit into 80 chars (even though there's plenty of screen width). See here ...

image

See all the space on the right? I can't seem to get the table to expand any farther right. I've tried the width and expand options, to no effect. I saw where the code is using shutil to get the screen dimensions. Here's the output of that on my screen:

$ python
Python 3.7.5 (default, Nov 24 2019, 09:32:14) 
[GCC 6.3.0 20170516] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> import shutil
>>> shutil.get_terminal_size()
os.terminal_size(columns=158, lines=48)

Your thoughts appreciated.

from rich.

willmcgugan avatar willmcgugan commented on May 21, 2024

Strange. The Console class calls shutil.get_terminal_size() under the hood.

How are you constructing Console? If you set width or height it will override the auto-detected terminal size.

What should happen is that if you set expand=True on the Table constructor it will fit the table to the terminal size. If you could post the code you used to construct the Console and Table, I may be able to figure it out.

from rich.

bwalrond avatar bwalrond commented on May 21, 2024

Here's my code ....

       self.console = Console()                                                                                                                              
 
    def outputColumnStats(self):                                                                                                                              
                                                                                                                                                              
        table = Table(show_header=True, box=box.ROUNDED, header_style="bold magenta", expand=True)                                                            
        table.add_column(header="Column", style="dim", width=9, no_wrap=True)                                                                                 
        table.add_column(header="str",justify="right", width=4, no_wrap=True)                                                                                 
        table.add_column(header="shrt",justify="right", width=5, no_wrap=True)                                                                                
        table.add_column(header="int",justify="right", width=4, no_wrap=True)                                                                                 
        table.add_column(header="long",justify="right", width=4, no_wrap=True)                                                                                
        table.add_column(header="flt",justify="right", width=3, no_wrap=True)                                                                                 
        table.add_column(header="date",justify="right", width=4 )                                                                                             
        table.add_column(header="dttm",justify="right", width=4, no_wrap=True)                                                                                
        table.add_column(header="null",justify="right", width=4, no_wrap=True)                                                                                
                                                                                                                                                              
                                                                                                                                                              
        for k,rows in self.col_stats.items():                                                                                                                 
            vals = [ "0" for r in range(8) ]                                                                                                                  
            for r in rows:                                                                                                                                    
                if r[0] == "string":                                                                                                                          
                    vals[0] = f"{r[1]:,}"                                                                                                                     
                elif r[0] == "short":                                                                                                                         
                    vals[1] = f"{r[1]:,}"                                                                                                                     
                elif r[0] == "integer":                                                                                                                       
                    vals[2] = f"{r[1]:,}"                                                                                                                     
                elif r[0] == "long":                                                                                                                          
                    vals[3] = f"{r[1]:,}"                                                                                                                     
                elif r[0] == "float":                                                                                                                         
                    vals[4] = f"{r[1]:,}"                                                                                                                     
                elif r[0] == "date":                                                                                                                          
                    vals[5] = f"{r[1]:,}"                                                                                                                     
                elif r[0] == "datetime":                                                                                                                      
                    vals[6] = f"{r[1]:,}"                                                                                                                     
                elif r[0] == "null":                                                                                                                          
                    vals[7] = f"{r[1]:,}"                                                                                                                     
                                                                                                                                                              
            table.add_row(k[:-7], *vals)                                                                                                                      
                                                                                                                                                              
        self.console.print(table)                                                                                                                             
                                                                                                                                                              
        return

Thanks.

from rich.

willmcgugan avatar willmcgugan commented on May 21, 2024

Ah. I think that's because you have explicitly set the widths for each column. You'll need at least one column without an explicit width for Rich to expand the table.

The Table class will calculate a minimum width for each column, so you might want to omit the width entirely. You could also set a value for ratio on a column to configure how it expands.

from rich.

bwalrond avatar bwalrond commented on May 21, 2024

Tried it, no luck. Seems like no matter what I do, I can't get the table to be wider than 80 chars.

from rich.

willmcgugan avatar willmcgugan commented on May 21, 2024

Running out of suggestions. Obviously works here, but then I'm on OSX. Will try a Linux box soon.

  • Could you print the result of self.console.size and check that matches your terminal size.
  • Run python -m rich.table which should render a table the full width of the terminal.

Just noticed you used os.get_terminal_size and not shutil.get_terminal_size. The later respects COLUMNS and LINES env vars. If your COLUMNS is set to 80, that would explain it.

from rich.

bwalrond avatar bwalrond commented on May 21, 2024

Here ya go ...

$ python
Python 3.7.5 (default, Nov 24 2019, 09:32:14) 
[GCC 6.3.0 20170516] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> from rich.console import Console
>>> console = Console()
>>> console.size
ConsoleDimensions(width=142, height=38)
>>> 

And output of python -m rich.table ...

$ python -m rich.table
Traceback (most recent call last):
  File "/home/bill.walrond/.pyenv/versions/3.7.5/lib/python3.7/runpy.py", line 193, in _run_module_as_main
    "__main__", mod_spec)
  File "/home/bill.walrond/.pyenv/versions/3.7.5/lib/python3.7/runpy.py", line 85, in _run_code
    exec(code, run_globals)
  File "/home/bill.walrond/.pyenv/versions/3.7.5/lib/python3.7/site-packages/rich/table.py", line 496, in <module>
    from .markdown import Markdown
  File "/home/bill.walrond/.pyenv/versions/3.7.5/lib/python3.7/site-packages/rich/markdown.py", line 4, in <module>
    from commonmark.blocks import Parser
ModuleNotFoundError: No module named 'commonmark'

Look like a module error. I'll try to debug this more when I get some time.

from rich.

willmcgugan avatar willmcgugan commented on May 21, 2024

Any chance you can try with the latest version (v0.6.0)? Still haven't managed to reproduce this.

from rich.

feluelle avatar feluelle commented on May 21, 2024

I am seeing the same issue on OSX when I run pytest. It creates a new line char after 78 chars. The total would match then 80 as well.

In my terminal I see a different output longer than 80 chars per line.

EDIT:

Solved it by adding -s flag to pytest.

from rich.

salahxgamer avatar salahxgamer commented on May 21, 2024

I had similar problem where the console intance created didn't have the size of my actual console.
it turned out because i was using cmder, and when running my script from a split console that was smaller, my script opens up in a new full screen console but with old one's dimensions. all in all it was not a fault on rich's end, just putting this out there

from rich.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.