question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

[help] How can I write my output into a file while keeping color

See original GitHub issue

Is it possible to write our output into a file while keeping the color. And when we do less -R see it with color?

I have tried following:

from rich.console import Console
from datetime import datetime

with open("report.txt", "wt") as report_file:
    console = Console(file=report_file)
    console.rule(f"Report Generated {datetime.now().ctime()}")
    console.print("[blue]hello[/blue]")
    console.file.write("[blue]hello[/blue]")

but hello is written without color into the report.txt file.


When I try console.file.write("[blue]hello[/blue]")

screenshot for cat report.txt | less -R:

image

Issue Analytics

  • State:closed
  • Created 2 years ago
  • Comments:6

github_iconTop GitHub Comments

0reactions
github-actions[bot]commented, Sep 18, 2021

Did I solve your problem?

Consider sponsoring the ongoing work on Rich and Textual.

Or buy me a coffee to say thanks.

Will McGugan

Read more comments on GitHub >

github_iconTop Results From Across the Web

How to write stdout to file with colors? - Stack Overflow
I'd like to know if there's a way (in Linux) to write the output to a file with colors. I'm trying to use...
Read more >
How to Write Stdout to a File with Colors | Baeldung on Linux
In this tutorial, we'll cover some tools we can use to write standard output to files while preserving their colors.
Read more >
Maintain color from script output for later viewing
You can use the less -R <filename>.<fileformat> command to open your output file ...
Read more >
How to write Stdout to a file with colors? - Tutorialspoint
To create some coloured output, we'll use the echo command; to store the output, we'll pipe it with the tee command. $ echo...
Read more >
Saving output of a grep into a file with colors - Ask Ubuntu
Now, when you print the file to the console it will be printed with the colors, because Bash interprets those characters as "use...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found