Welcome!
This is the community forum for my apps Pythonista and Editorial.
For individual support questions, you can also send an email. If you have a very short question or just want to say hello — I'm @olemoritz on Twitter.
Colored text
-
Hi! I wanted to color my text in pythonista, but, for some reason, there’s no colorama module! Here’s the code that should work:
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') ``` 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')
I also tried this code:
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)
and it returns an error about neither the colorama module nor the termcolor module existing. Is it just because there is no tkinter? Also, is there any way to work around this?
-
When I tried this code:
def prRed(skk): print("\033[91m {}\033[00m" .format(skk)) def prGreen(skk): print("\033[92m {}\033[00m" .format(skk)) def prYellow(skk): print("\033[93m {}\033[00m" .format(skk)) def prLightPurple(skk): print("\033[94m {}\033[00m" .format(skk)) def prPurple(skk): print("\033[95m {}\033[00m" .format(skk)) def prCyan(skk): print("\033[96m {}\033[00m" .format(skk)) def prLightGray(skk): print("\033[97m {}\033[00m" .format(skk)) def prBlack(skk): print("\033[98m {}\033[00m" .format(skk)) prCyan("Hello World, ") prYellow("It's") prGreen("Bumbo") prRed("For") prGreen("Cactoni")
It does not acknowledge that I am using the backslash and 033[ code for color. Please help?
It returns this, with no color:
[96m Hello World, [00m
[93m It's[00m
[92m Bumbo[00m
[91m For[00m
[92m Cactoni[00m -
The pythonista console does not support ansi color codes. There are some Obj workarounds, but that are generally not portable.
Stash, on the other hand, does support most ansi color codes. If you need color, you can run your script in stash.
-
@JonB
Ok, thanks!Btw, is Stash an app on the App Store or something?
-
@Bumbo-Cactoni Please try this, for the fun
from objc_util import * import ui UIColor = ObjCClass('UIColor') NSMutableAttributedString = ObjCClass('NSMutableAttributedString') UIFont = ObjCClass('UIFont') ansi_colors = {'[91m':'red', '[92m':'green', '[93m':'yellow', '[94m':'mediumpurple', '[95m':'purple', '[96m':'cyan', '[97m':'lightgray', '[98m':'black'} @on_main_thread def myprint(txt): from objc_util import ObjCClass win = ObjCClass('UIApplication').sharedApplication().keyWindow() main_view = win.rootViewController().view() ret = '' font = UIFont.fontWithName_size_('Menlo', 15) def analyze(v): for tv in v.subviews(): if 'OMTextView' in str(tv._get_objc_classname()): su = tv.superview() if 'OMTextEditorView' in str(su._get_objc_classname()): continue # tv = console is a OMTextView baseClass = UIScrollView #print(dir(tv)) txt_list = txt.split('\033') for ele in txt_list: if ele == '': continue #print('ele',ele) c = ansi_colors[ele[:4]] t = ele[4:] color = UIColor.colorWithRed_green_blue_alpha_(*ui.parse_color(c)) attr_str = NSMutableAttributedString.alloc().initWithString_(t) attributes = {ns('NSColor'):color, ns('NSFont'):font} attr_str.setAttributes_range_(attributes, NSRange(0, len(t))) tv.appendAttributedText_(attr_str) ret = analyze(tv) if ret: return ret ret = analyze(main_view) if __name__ == '__main__': print() myprint("\033[91m {} \033[94m {}".format('this is red','this is mediumpurple'))
-
@cvp
Oh, so that’s how to do it? Ok, thanks! -
Stash is.a pythonista bash clone
-
@Bumbo-Cactoni, to point out the obvious, just in case, if all you want is colored text in the Pythonista console, you can use the relevant bit of @cvp’s code, skip all the ANSI mush, and just use Pythonista’s color names or the color picker.
-
This post is deleted!