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

  • Home
  • SEARCH
  • 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

When browsing ASP.NET MVC source code in codeplex , I found it is common
I'm building a per-user file browsing/uploading application using Django and when I run this
From browsing on this site and elsewhere, I've learned that serving websites as XHTML
While browsing some code I found a call to OpenPrinter() . The code compiles
I was browsing this thread , which has good recommendation but a bit too
I was browsing Scott Hanselman's Developer Interview question list , and ran across this
In my Django templates, I have a couple pieces of code that are like
Browsing through the git documentation, I can't see anything analogous to SVN's commit hooks
While browsing with Chrome, I noticed that it responds extremely fast (in comparison with
Is browsing speed depends on browser (like IE,firefox)? If it depends on browser 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.