colouring strings - Printable Version +- Python Forum (https://python-forum.io) +-- Forum: Python Coding (https://python-forum.io/forum-7.html) +--- Forum: General Coding Help (https://python-forum.io/forum-8.html) +--- Thread: colouring strings (/thread-21149.html) |
colouring strings - newbieAuggie2019 - Sep-17-2019 Hi! Due to time constraints, I'm working with a lot of issues at the same time: f-strings, functions, loops... and I was wondering... with so many formatting ways, is there a straightforward way to format the text of a string with characteristics like font type, bold, italics, underlined, size, colour, etc. in a similar way with tags, like in here?: print("\n\nThis is a [b]test[/b] \ to [color=red]check[/color] and [size=8]other[/size]\ [font=Tahoma]formatting[/font]types.")That little program produces the following failed formatted string test: All the best,
RE: colouring strings - wavic - Sep-17-2019 https://pypi.org/project/colorama/ ? https://python-prompt-toolkit.readthedocs.io/en/master/pages/printing_text.html ? I'm not sure how to deal with the text size RE: colouring strings - newbieAuggie2019 - Sep-17-2019 (Sep-17-2019, 03:14 AM)wavic Wrote: https://pypi.org/project/colorama/ ? Thank you very much indeed! I had seen about colorama, searching for colouring strings, but I didn't know about termcolor. I was a bit worried about these kinds of packages, because I'm a newbie, and you need to import them and make other modifications inside your programs to make them work... and I was right... I don't know what I'm doing wrong, but I cannot make it work in my Windows 10. I'm using Python 3.7.4 and somewhere I read that it's with batteries included... Well, I beg to differ... I need colour in my world, and I think it should be included within the original Python download package, but oh, blimey, I cannot do something about it. I'm going to show what I have done, and maybe somebody is kind enough to indicate me what I'm doing wrong. Okay, first, I gave colorama a try, so I used pip installation, which seems to have been successful: Then I tried a test program: import colorama from colorama import init init() from colorama import Fore, Back, Style print(Fore.RED + 'some red text') print(Back.GREEN + 'and with a green background') print(Style.DIM + 'and in dim text') print(Style.RESET_ALL) print('back to normal now') print('\033[31m' + 'some red text') print('\033[30m') # and reset to default colorwith the following failed output: Then I tried with a different test program from a different site:import colorama # If using Windows, init() will cause anything sent to stdout or stderr # will have ANSI color codes converted to the Windows versions. Hooray! # If you are already using an ANSI compliant shell, it won't do anything colorama.init() # Now regular ANSI codes should work, even in Windows CLEAR_SCREEN = '\033[2J' RED = '\033[31m' # mode 31 = red forground RESET = '\033[0m' # mode 0 = reset print(CLEAR_SCREEN + RED + 'Welcome!' + RESET)producing an equally failed output: I tried with a different test program from another site:import colorama from colorama import Fore, Back, Style colorama.init() # Set the color semi-permanently print(Fore.CYAN) print("Text will continue to be cyan") print("until it is reset or changed") print(Style.RESET_ALL) # Colorize a single line and then reset print(Fore.RED + 'You can colorize a single line.' + Style.RESET_ALL) # Colorize a single word in the output print('Or a single ' + Back.GREEN + 'words' + Style.RESET_ALL + ' can be highlighted') # Combine foreground and background color print(Fore.BLUE + Back.WHITE) print('Foreground, background, and styles can be combined') print("========== ") print(Style.RESET_ALL) print('If unsure, reset everything back to normal.')and another fail: So I decided then to give termcolor a try, and also it seems that it was a successful installation:Then I used the following test program from the termcolor site: import sys from termcolor import colored, cprint text = colored('Hello, World!', 'red', attrs=['reverse', 'blink']) print(text) cprint('Hello, World!', 'green', 'on_red') print_red_on_cyan = lambda x: cprint(x, 'red', 'on_cyan') print_red_on_cyan('Hello, World!') print_red_on_cyan('Hello, Universe!') for i in range(10): cprint(i, 'magenta', end=' ') cprint("Attention!", 'red', attrs=['bold'], file=sys.stderr)with the following once more failed attempt: Thanks in advance, any help would be appreciated.All the best, I just noticed that indeed the last part of the output shown for termcolor was indeed printed in red, but just the last part. And as it says Attention!, probably my mind played a trick on me. It was shown in red, not in bold type as it should, but at least it could help to find an answer. This was printed in red (like an error message) at the end of the output (the rest of the output was shown in the regular blue coloured font type on a white background colour, in my Python 3.7.4 Shell running on Windows 10: <-[1m<-[31mAttention!<-[0m RE: colouring strings - snippsat - Sep-17-2019 Need a better Terminal shell than cmd/Poweshell. I only use cmder | Github Also remember to do .\cmder.exe /REGISTER ALL .So get a shortcut in file explorer,to go directly to cmder. Test term.py is your latest code.RE: colouring strings - newbieAuggie2019 - Sep-17-2019 As it seems that only the last part was working I modified the program as: import sys from termcolor import colored, cprint cprint("Attention!", 'red', attrs=['bold'], file=sys.stderr) cprint("Attention!", 'green', attrs=['bold'], file=sys.stderr) cprint("Attention!", 'yellow', attrs=['bold'], file=sys.stderr) cprint("Attention!", 'cyan', attrs=['bold'], file=sys.stderr) cprint("Attention!", 'grey', attrs=['bold'], file=sys.stderr) cprint("Attention!", 'blue', attrs=['bold'], file=sys.stderr) cprint("Attention!", 'white', attrs=['bold'], file=sys.stderr) cprint("Attention!", 'magenta', attrs=['bold'], file=sys.stderr)but then everything was printed in red, also with the annoying arrows, brackets and ANSI code numbers: <-[1m<-[31mAttention!<-[0m <-[1m<-[32mAttention!<-[0m <-[1m<-[33mAttention!<-[0m <-[1m<-[36mAttention!<-[0m <-[1m<-[30mAttention!<-[0m <-[1m<-[34mAttention!<-[0m <-[1m<-[37mAttention!<-[0m <-[1m<-[35mAttention!<-[0m (Sep-17-2019, 04:42 PM)snippsat Wrote: Need a better Terminal shell than cmd/Poweshell.Thanks a lot! It looks promising, but when executing Cmder.exe, I received a message from my Avast antivirus saying: Blocked threat We have blocked init.bat because it was infected with IDP.ALEXA.51 What should I do??? RE: colouring strings - snippsat - Sep-17-2019 (Sep-17-2019, 04:48 PM)newbieAuggie2019 Wrote: What should I do???Put cmder folder in Global Whitelist. RE: colouring strings - newbieAuggie2019 - Sep-17-2019 (Sep-17-2019, 05:13 PM)snippsat Wrote:Thanks again!(Sep-17-2019, 04:48 PM)newbieAuggie2019 Wrote: What should I do???Put cmder folder in Global Whitelist. I just used the link you provided and there, it says to be sure it doesn't contain any viruses by checking in the free VirusTotal site, and although almost Quote:No engines detected this filetwo of them were Quote:Unable to process file type, and most importantly, there was the following message on the community section:
It seems that I'm not alone having that message: GitHub - IDP.ALEXA.51 threat RE: colouring strings - snippsat - Sep-17-2019 It's a just false positive,the two engine that defect something is mediocre/bad Chinese AV's. RE: colouring strings - wavic - Sep-17-2019 Windows is a bit tricky about the text formatting. Try prompt_toolkit. It's more mature. RE: colouring strings - newbieAuggie2019 - Sep-17-2019 (Sep-17-2019, 04:42 PM)snippsat Wrote: I only use cmder | GithubThanks again, I'm trying hard, but it seems I'm useless. (Sep-17-2019, 04:42 PM)snippsat Wrote: Also remember to doHow do you that? (Sep-17-2019, 04:42 PM)snippsat Wrote:That image looks really nice, but how do you get it? I didn't see any information about it. Finally, I managed to install it, but I think I did it wrong, because I tried your suggestion (term.py) and others from the cmder site, and I'm not sure I have done it right: Maybe it's something related to paths. I always have problems with paths (I had them with Python when I tried to install it in a different folder than the root directory)... All the best, (Sep-17-2019, 03:14 AM)wavic Wrote: https://pypi.org/project/colorama/ ?Did you change the links?... I would have sworn they were about colorama and termcolor... Maybe I'm becoming crazy... (Sep-17-2019, 03:14 AM)wavic Wrote: https://python-prompt-toolkit.readthedocs.io/en/master/pages/printing_text.html ?Thank you again. I'll give it a try too, as I'm becoming rather frustrated at my incompetence to make the previous ones work... I just want to put a bit of colour in my life , coding in a nice environment and producing a cool output... All the best, |