New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Using termcolor in PyScript doesn't affect console output #1288
Comments
I'm not super deep in the code for the terminal that shows up when you |
I think print("\x1b[1mHello\x1b[0m \x1b[31mWorld\x1b[0m") That makes me think that if termcolor cannot be sure about which kind of terminal is executing it just doesn't do anything (doesn't transform the text) but in case we'd receive special chars, I believe the terminal won't be able to handle these, while the devtools console would work just as fine. Maybe waiting for @JeffersGlass PR is indeed a good idea (imho). |
Very interesting. I'm just happy to hear some thoughts on this. I think I'll go with WebReflection's comment there about waiting for rich implementation. Thanks again, everyone. |
No problem! Closing this one then. Thanks! :) |
Another option might be using a JS or Python library that converts ANSI escape sequences of text to HTML. The most popular Python one seems to be ansi2html, but it's API is a bit odd - for a given sequence, you can convert it either to an entire HTML page (with head/body tags) or a snippet that wraps the styled text in The JavaScript options for ansi-to-HTML conversion seem more promising - here's a short demo using the ansi_up library: <!--index.html-->
<head>
<script src="https://cdn.jsdelivr.net/npm/ansi_up@5.1.0/ansi_up.js"></script>
<script defer src="https://pyscript.net/releases/2023.03.1/pyscript.js"></script>
<link rel="stylesheet" href="https://pyscript.net/releases/2023.03.1/pyscript.css">
</head>
<body>
<py-config>
packages = ['termcolor']
</py-config>
<py-script src="termdemo.py"></py-script>
</body>
</html> #termdemo.py
import os
import termcolor
import js
ansi_up = js.AnsiUp.new() #Create new coverter object
os.environ['FORCE_COLOR'] = "True" #Force termcolor to output ANSI sequences
raw = termcolor.colored("Hello", "green", "on_blue") #Create text with ansi sequences embedded
converted = ansi_up.ansi_to_html(raw) #HTML-styled text
display(HTML(converted)) #Send HTML to DOM without escaping
print(converted) #Show escaped HTML in terminal, for interest
Indeed, termcolor uses some environment variables to determine whether it should be output escape codes or not. You can ensure it does in PyScript/Pyodide by using Rich has a much more involved method of detecting what terminal it thinks it's in, but it seems ANSI escape codes can generally be forced in the browser with |
makes sense, thanks for explaining! I am totally up for that being said, the |
yes, the current py-terminal is deliberately designed to be dumb and not support any escape sequence. I think that the "proper" solution is to use xterm.js, as Jeff is doing. That said, we could decide to support ANSI escape sequence for colors also in the dumb terminal: in theory it feels wrong (because people should respect the |
@antocuni we can always make it optional via an attribute or accessor so that |
I think for the moment I'd agree that the current behavior (escape sequences don't do anything) is probably fine as a default. The expanded optional behavior of supporting colors feels like a great thing to add to a community (external) plugin at some point. |
I'm not opposed to have a terminal which supports ANSI colors (either builtin or via a plugin), but then we need to think carefully. I doubt that such a terminal exists, because AFAIK most of them supports at least basic cursor movement (which I guess we don't want to emulate). So we could probably invent a new one -- let's call it |
Checklist
What happened?
When I run a simple PyScript program such as the one below, the console output on the webpage doesn't seem to be affected by the package 'termcolor'.
The output is the standard white instead of an expected red.
What browsers are you seeing the problem on? (if applicable)
Chrome
Console info
Additional Context
No response
The text was updated successfully, but these errors were encountered: