Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 3404200
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 18, 20262026-05-18T05:21:11+00:00 2026-05-18T05:21:11+00:00

I was browsing the Django source code and I saw this function: def colorize(text=”,

  • 0

I was browsing the Django source code and I saw this function:

def colorize(text='', opts=(), **kwargs):
    """
    Returns your text, enclosed in ANSI graphics codes.

    Depends on the keyword arguments 'fg' and 'bg', and the contents of
    the opts tuple/list.

    Returns the RESET code if no parameters are given.

    Valid colors:
    'black', 'red', 'green', 'yellow', 'blue', 'magenta', 'cyan', 'white'

    Valid options:
    'bold'
    'underscore'
    'blink'
    'reverse'
    'conceal'
    'noreset' - string will not be auto-terminated with the RESET code

    Examples:
    colorize('hello', fg='red', bg='blue', opts=('blink',))
    colorize()
    colorize('goodbye', opts=('underscore',))
    print colorize('first line', fg='red', opts=('noreset',))
    print 'this should be red too'
    print colorize('and so should this')
    print 'this should not be red'
    """
    code_list = []
    if text == '' and len(opts) == 1 and opts[0] == 'reset':
        return '\x1b[%sm' % RESET       
    for k, v in kwargs.iteritems(): 
        if k == 'fg':
            code_list.append(foreground[v]) 
        elif k == 'bg':
            code_list.append(background[v]) 
    for o in opts:
        if o in opt_dict:
            code_list.append(opt_dict[o])   
    if 'noreset' not in opts:
        text = text + '\x1b[%sm' % RESET
    return ('\x1b[%sm' % ';'.join(code_list)) + text

I removed it out of the context and placed in another file just to try it, the thing is that it doesn’t seem to colour the text I pass it. It might be that I don’t understand it correctly but isn’t it supposed to just return the text surrounded with ANSI graphics codes which than the terminal will convert to actual colours.

I tried all the given examples of calling it, but it just returned the argument I specified as a text.

I’m using Ubuntu so I think the terminal should support colours.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-18T05:21:12+00:00Added an answer on May 18, 2026 at 5:21 am

    It’s that you have many terms undefined, because it relies on several variables defined outside of the function.

    Instead just

    import django.utils.termcolors as termcolors
    red_hello = termcolors.colorize("Hello", fg='red') # '\x1b[31mHello\x1b[0m'
    print red_hello
    

    Or just also copy the first few lines of django/utils/termcolors.py specifically:

    color_names = ('black', 'red', 'green', 'yellow', 'blue', 'magenta', 'cyan', 'white')
    foreground = dict([(color_names[x], '3%s' % x) for x in range(8)])
    background = dict([(color_names[x], '4%s' % x) for x in range(8)])
    RESET = '0'
    
    def colorize( ... ):
        ...
    print colorize("Hello", fg='red') # '\x1b[31mHello\x1b[0m'
    

    Also note:

    >>> from django.utils.termcolors import colorize
    >>> red_hello = colorize("Hello", fg="red")
    >>> red_hello # by not printing; it will not appear red; special characters are escaped
    '\x1b[31mHello\x1b[0m'
    >>> print red_hello # by print it will appear red; special characters are not escaped
    Hello
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Browsing through the source code of Microsoft's sample StockTrader application, I found this snippet
Was browsing the jQuery source code when I met this line: jQuery(this)[ state ?
While browsing the source code of the Scala API, I met this package definition
browsing some html code I find a text input coded as: <input type=text name=Email<%=orderRow.ID
As I'm browsing through a Rails source code, it contains the line: @@autoloads =
Browsing through Guava libraries I saw this weird signature on the readLines method of
While browsing System.Zip (Delphi XE2) to see how it works, I found this function:
I'm building a per-user file browsing/uploading application using Django and when I run this
Browsing through Coding Horror, I saw this article on removing the user field from
Browsing the ruleby source code, I noticed that they were calling Container(:and) , which

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.