logo
down
shadow

Is it possible to print from Python using non-ANSI colors?


Is it possible to print from Python using non-ANSI colors?

Content Index :

Is it possible to print from Python using non-ANSI colors?
Tag : python , By : wraith
Date : January 12 2021, 08:33 AM

To fix this issue Well, here's one for the 256-color xterm palette, in Python 3. Escape-code/hex value map from here. (I can't remember why I called it "aixterm" - as Thomas Dickey pointed out in a comment, this is actually for xterm.) The xterm FAQ on colors may also be helpful.
import myaixterm, call aix_init(), then print(aix_fg(128),'whatever',aix_normal()).
# myaixterm.py: custom color mappings for the xterm 256-color palette
# or RGB888.
# Copyright (c) cxw 2015--2017

import itertools
import csv

_DEF_COLOR_FN='myaixterm-db.txt'

aix_colors={}

def aix_fg(color):
    """ Returns a string that will set the foreground to _color_, which can
        be a color number (0..255) or a name in aix_colors. """
    if isinstance(color,str):
        colornum=aix_colors[color]
    else:
        colornum=color
    return '\033[38;5;%dm'%colornum

def aix_bg(color):
    """ Returns a string that will set the background to _color_, which can
        be a color number (0..255) or a name in aix_colors. """
    if isinstance(color,str):
        colornum=aix_colors[color]
    else:
        colornum=color
    return '\033[48;5;%dm'%colornum

def aix_fg_rgb(r,g,b):
    """Returns a string to set foreground.  r, g, b should be between 0 and 255."""
    return '\033[38;2;%d;%d;%dm' % (r,g,b)

def aix_bg_rgb(r,g,b):
    """Returns a string to set background.  r, g, b should be between 0 and 255."""
    return '\033[48;2;%d;%d;%dm' % (r,g,b)

def aix_normal():
    """ Returns a string that will set the foreground and background
        to their default colors. """
    return '\033[0m'

def aix_init(fn=_DEF_COLOR_FN):
    with open(fn,'r') as fd:
        reallines=itertools.filterfalse(lambda r: r.startswith('#'), fd)
        for row in csv.DictReader(reallines,
                                    fieldnames=['r','g','b','n'],
                                    restkey='names'):
            for name in row['names']:
                aix_colors[name]=int(row['n'])
            #end foreach name
        #end foreach row
    #end with
# end aix_init
# modified by cxw from colortest perl script
#   at http://www.vim.org/scripts/script.php?script_id=1349
# original copyright:
# by entheon, do whatever the hell you want with this file

0,0,0,0,black,neutrala
191,0,0,1,red
0,191,0,2,green
191,191,0,3,yellow
0,0,191,4,blue
191,0,191,5,magenta
0,191,191,6,cyan
191,191,191,7,gray77,neutraly
64,64,64,8,gray27,neutralh
255,64,64,9,brightred
64,255,64,10,brightgreen
255,255,64,11,brightyellow
96,96,255,12,brightblue
255,64,255,13,brightmagenta
64,255,255,14,brightcyan
255,255,255,15,white,neutralzf
00,00,00,16,cubeblack
00,00,95,17,color17
00,00,135,18,color18
00,00,175,19,color19
00,00,215,20,color20
00,00,255,21,fullblue
00,95,00,22,color22
00,95,95,23,color23
00,95,135,24,color24
00,95,175,25,color25
00,95,215,26,color26
00,95,255,27,color27
00,135,00,28,color28
00,135,95,29,color29
00,135,135,30,color30
00,135,175,31,color31
00,135,215,32,color32
00,135,255,33,color33
00,175,00,34,color34
00,175,95,35,color35
00,175,135,36,color36
00,175,175,37,color37
00,175,215,38,color38
00,175,255,39,color39
00,215,00,40,color40
00,215,95,41,color41
00,215,135,42,color42
00,215,175,43,color43
00,215,215,44,color44
00,215,255,45,color45
00,255,00,46,fullgreen
00,255,95,47,color47
00,255,135,48,color48
00,255,175,49,color49
00,255,215,50,color50
00,255,255,51,fullcyan
95,00,00,52,color52
95,00,95,53,color53
95,00,135,54,color54
95,00,175,55,color55
95,00,215,56,color56
95,00,255,57,color57
95,95,00,58,color58
95,95,95,59,gray40,neutrall
95,95,135,60,color60
95,95,175,61,color61
95,95,215,62,color62
95,95,255,63,color63
95,135,00,64,color64
95,135,95,65,color65
95,135,135,66,color66
95,135,175,67,color67
95,135,215,68,color68
95,135,255,69,color69
95,175,00,70,color70
95,175,95,71,color71
95,175,135,72,color72
95,175,175,73,color73
95,175,215,74,color74
95,175,255,75,color75
95,215,00,76,color76
95,215,95,77,color77
95,215,135,78,color78
95,215,175,79,color79
95,215,215,80,color80
95,215,255,81,color81
95,255,00,82,color82
95,255,95,83,color83
95,255,135,84,color84
95,255,175,85,color85
95,255,215,86,color86
95,255,255,135,color135
135,00,00,88,color88
135,00,95,89,color89
135,00,135,90,color90
135,00,175,91,color91
135,00,215,92,color92
135,00,255,93,color93
135,95,00,94,color94
135,95,95,95,color95
135,95,135,96,color96
135,95,175,97,color97
135,95,215,98,color98
135,95,255,99,color99
135,135,00,100,color100
135,135,95,101,color101
135,135,135,102,gray56,neutralq
135,135,175,103,color103
135,135,215,104,color104
135,135,255,105,color105
135,175,00,106,color106
135,175,95,107,color107
135,175,135,108,color108
135,175,175,109,color109
135,175,215,110,color110
135,175,255,111,color111
135,215,00,112,color112
135,215,95,113,color113
135,215,135,114,color114
135,215,175,115,color115
135,215,215,116,color116
135,215,255,117,color117
135,255,00,118,color118
135,255,95,119,color119
135,255,135,120,color120
135,255,175,121,color121
135,255,215,122,color122
135,255,255,123,color123
175,00,00,124,color124
175,00,95,125,color125
175,00,135,126,color126
175,00,175,127,color127
175,00,215,128,color128
175,00,255,129,color129
175,95,00,130,color130
175,95,95,131,color131
175,95,135,132,color132
175,95,175,133,color133
175,95,215,134,color134
175,95,255,135,color135
175,135,00,136,color136
175,135,95,137,color137
175,135,135,138,color138
175,135,175,139,color139
175,135,215,140,color140
175,135,255,141,color141
175,175,00,142,color142
175,175,95,143,color143
175,175,135,144,color144
175,175,175,145,gray71,neutralv
175,175,215,146,color146
175,175,255,147,color147
175,215,00,148,color148
175,215,95,149,color149
175,215,135,150,color150
175,215,175,151,color151
175,215,215,152,color152
175,215,255,153,color153
175,255,00,154,color154
175,255,95,155,color155
175,255,135,156,color156
175,255,175,157,color157
175,255,215,158,color158
175,255,255,159,color159
215,00,00,160,color160
215,00,95,161,color161
215,00,135,162,color162
215,00,175,163,color163
215,00,215,164,color164
215,00,255,165,color165
215,95,00,166,color166
215,95,95,167,color167
215,95,135,168,color168
215,95,175,169,color169
215,95,215,170,color170
215,95,255,171,color171
215,135,00,172,color172
215,135,95,173,color173
215,135,135,174,color174
215,135,175,175,color175
215,135,215,176,color176
215,135,255,177,color177
215,175,00,178,color178
215,175,95,179,color179
215,175,135,180,color180
215,175,175,181,color181
215,175,215,182,color182
215,175,255,183,color183
215,215,00,184,color184
215,215,95,185,color185
215,215,135,186,color186
215,215,175,1135,color1135
215,215,215,188,gray86,neutralzb
215,215,255,189,color189
215,255,00,190,color190
215,255,95,191,color191
215,255,135,192,color192
215,255,175,193,color193
215,255,215,194,color194
215,255,255,195,color195
255,00,00,196,fullred
255,00,95,197,color197,awesome,rosebush
255,00,135,198,color198,hotpink
255,00,175,199,color199,beachtowel
255,00,215,200,color200,
255,00,255,201,fullmagenta
255,95,00,202,color202,fullorange
255,95,95,203,color203,melon
255,95,135,204,color204,petal
255,95,175,205,color205,pretty
255,95,215,206,color206
255,95,255,207,color207
255,135,00,208,color208,sunset
255,135,95,209,color209,flesh1
# ^^^ tan?
255,135,135,210,color210,lipstick
255,135,175,211,color211
255,135,215,212,color212
255,135,255,213,color213
255,175,00,214,color214,sun
255,175,95,215,color215,flesh2
255,175,135,216,color216,flesh3
255,175,175,217,color217,softpink,pillowpink
255,175,215,218,color218
255,175,255,219,color219
255,215,00,220,color220,sunrise
255,215,95,221,color221,pastelyellow
255,215,135,222,color222,flesh4
255,215,175,223,color223,flesh5
255,215,215,224,color224
255,215,255,225,color225
255,255,00,226,fullyellow
255,255,95,227,color227
255,255,135,228,color228
255,255,175,229,color229,parchment
255,255,215,230,color230,ivory
255,255,255,231,cubewhite,ultrawhite
8,8,8,232,gray2,neutralb
18,18,18,233,gray5,neutralc
28,28,28,234,gray10,neutrald
38,38,38,235,gray15,neutrale
48,48,48,236,gray20,neutralf
58,58,58,237,gray24,neutralg
68,68,68,238,gray29,neutrali
78,78,78,239,gray33,neutralj
88,88,88,240,gray37,neutralk
98,98,98,241,gray42,neutralm
108,108,108,242,gray46,neutraln
118,118,118,243,gray50,neutralo
128,128,128,244,gray54,neutralp
138,138,138,245,gray57,neutralr
148,148,148,246,gray61,neutrals
158,158,158,247,gray65,neutralt
168,168,168,248,gray69,neutralu
178,178,178,249,gray73,neutralw
188,188,188,250,gray76,neutralx
198,198,198,251,gray80,neutralz
208,208,208,252,gray83,neutralza
218,218,218,253,gray87,neutralzc
228,228,228,254,gray91,neutralzd
238,238,238,255,gray94,neutralze

Comments
No Comments Right Now !

Boards Message :
You Must Login Or Sign Up to Add Your Comments .

Share : facebook icon twitter icon

Python module to enable ANSI colors for stdout on Windows?


Tag : python , By : Ambarish Singh
Date : March 29 2020, 07:55 AM
around this issue There are two python modules that are able to do this colorama and tendo.ansiterm module, which was originally written for waf.
By initial tests indicate that colorama is more mature, even if it requires two lines of code instead of one.
import sys
try:
   import colorama
   colorama.init()
except:
   try:
       import tendo.ansiterm
   except:
       pass

sys.stdout.write"\033[33mYellow Submarine"
sys.stderr.write"\033[31mred, red , wine!"

Python’s `str.format()`, fill characters, and ANSI colors


Tag : python , By : Mark W
Date : March 29 2020, 07:55 AM
this one helps. Python doesn't distinguish between 'normal' characters and ANSI colour codes, which are also characters that the terminal interprets.
In other words, printing '\x1b[92m' to a terminal may change the terminal text colour, Python doesn't see that as anything but a set of 5 characters. If you use print repr(line) instead, python will print the string literal form instead, including using escape codes for non-ASCII printable characters (so the ESC ASCII code, 27, is displayed as \x1b) to see how many have been added.

Visible ANSI escape sequences when running Emacs shell (these are not ANSI colors)


Tag : shell , By : ranja
Date : March 29 2020, 07:55 AM
With these it helps As you say, Emacs' eshell can't process escape codes -- it is a "dumb" terminal. But, your TERM variable setting is telling your shell that it IS capable of processing those escape codes.
Setting TERM to "dumb" when you're inside eshell should make those ugly escape codes go away.

Unable to get ANSI colors in PHP CLI without passing --ansi


Tag : php , By : jch
Date : March 29 2020, 07:55 AM
With these it helps I found the answer in a comment on another question.
https://serverfault.com/questions/591105/centos-colors-in-php-cli-applications#comment-698025

Jenkins + Python + MSBuild = No ANSI Colors


Tag : jenkins , By : T11M
Date : March 29 2020, 07:55 AM
Related Posts Related QUESTIONS :
  • Ignore Nulls in pandas map dictionary
  • How do I get scrap data from web pages using beautifulsoup in python
  • Variable used, golobal or local?
  • I have a regex statement to pull all numbers out of a text file, but it only finds 77 out of the 81 numbers in the file
  • How do I create a dataframe of jobs and companies that includes hyperlinks?
  • Detect if user has clicked the 'maximized' button
  • Does flask_login automatically set the "next" argument?
  • Indents in python 3
  • How to create a pool of threads
  • Pandas giving IndexError on one dataframe but not on another similar dataframe
  • Django Rest Framework - Testing client.login doesn't login user, ret anonymous user
  • Running dag without dag file in airflow
  • Filling across a specified dimension of a numpy array
  • Python populating dataframe in pandas from text files
  • How to interpolate a single ("non-piecewise") cubic spline from a set of data points?
  • Divide 2 integers (leetcode 29) - recursion issue
  • Can someone explain why do I get this output in Python?
  • How do I scrape pdf and html from search results without obvious url
  • Is there a way to automatically make a "collage" of plots with matplotlib?
  • How to combine multiple rows in pandas with shared column values
  • How do I get LOAD_CLASSDEREF instruction after dis.dis?
  • Django - How to add items to Bootstrap dropdown?
  • Linear Regression - Does the below implementation of ridge regression finding coefficient term using gradient method is
  • How to drop all rows in pandas dataframe with negative values?
  • Most Efficient Way to Find Closest Date Between 2 Dataframes
  • Execution error when Passing arguments to a python script using os.system. The script takes sys.argv arguments
  • Looping through a function
  • Create a plot for each unique ID
  • a thread python with 'while' got another thread never start
  • Solution from SciPy solve_ivp contains oscillations for a system of first-order ODEs
  • trigger python events driven by selenium controlled browser
  • Passing line-edits to a contextmanager to set validators
  • Python: globals().items() iterations try to change a dict
  • Is it possible to specify starting values for each parameter (instead of bounds) for scipy's differential evolution?
  • why datetime.now() and constructed datetime using all fields(like year,month...) of now has big timedelta?
  • MySQL multiple table UPDATE query using sqlalchemy core?
  • find if a semantic version is superset of of another version python
  • Type checking against dynamically created objects
  • Struggling with simple reverse function
  • Is there a function for finding the midpoint of n points on sklearn.neighbors.NearestNeighbors?
  • How to set max number of tweets to fetch
  • PYTHON 3.7.4 NOT USING SQLITE 3.29.0
  • How to replace Nan value with zeros in a numpy array?
  • How to speed up calculating variance among sparse matrix
  • cupy code is not fast enough compared with numpy
  • How to count frequency of select values in Python pandas dataframe
  • Scrape Span Text from Google
  • Python watchdog, watch a directory and rename file on event.modification
  • Filtering rows in DataFrame with dependent conditions
  • How to check if a character is a not a part of number or URL in string?
  • Compare corresponding elements of a list
  • Python misinterprets 3 character string as UTF-8 continuation byte
  • Merge two columns in Pandas
  • Side Effect error in Python in an online compiler
  • How to convert a navigation list with depth levels to a parent-child flat list?
  • Retrieving values from a paired key dictionary in Python
  • How to test the current text of a Tkinter text box widget before inserting new text after user clicks on a button?
  • computing the Cumulative Sum, where Sum can be reset by a condition
  • Where do you specify your API key when making a request with the Google API python library?
  • Pandas DataFrame, computing the Time Difference between one row and other row which satisfies a condition
  • shadow
    Privacy Policy - Terms - Contact Us © scrbit.com