Skip to content

Instantly share code, notes, and snippets.

@dideler
Last active May 23, 2024 12:37

Revisions

  1. dideler revised this gist Nov 30, 2018. 1 changed file with 3 additions and 3 deletions.
    6 changes: 3 additions & 3 deletions python-colours.md
    Original file line number Diff line number Diff line change
    @@ -5,14 +5,14 @@ Straight-up

    For example, using a dictionary:

    ```
    ```python
    ansi = {'underline': '\033[4m', 'bold': '\033[1m', 'end':'\033[0m'}
    print '{[bold]}Hello World{[end]}'.format(ansi, ansi)
    ```

    Or you can also create a class with enable/disable methods:

    ```
    ```python
    class bcolors:
    HEADER = '\033[95m'
    OKBLUE = '\033[94m'
    @@ -39,7 +39,7 @@ Libraries

    #### [termcolor](http://pypi.python.org/pypi/termcolor)
    E.g.:
    ```
    ```python
    from termcolor import colored

    print colored('hello', 'red'), colored('world', 'green')
  2. dideler revised this gist Oct 6, 2012. No changes.
  3. dideler created this gist Oct 1, 2012.
    56 changes: 56 additions & 0 deletions python-colours.md
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,56 @@
    Most (or all) of these methods make use of ANSI escape sequences.

    Straight-up
    -----------

    For example, using a dictionary:

    ```
    ansi = {'underline': '\033[4m', 'bold': '\033[1m', 'end':'\033[0m'}
    print '{[bold]}Hello World{[end]}'.format(ansi, ansi)
    ```

    Or you can also create a class with enable/disable methods:

    ```
    class bcolors:
    HEADER = '\033[95m'
    OKBLUE = '\033[94m'
    OKGREEN = '\033[92m'
    WARNING = '\033[93m'
    FAIL = '\033[91m'
    ENDC = '\033[0m'
    def disable(self):
    self.HEADER = ''
    self.OKBLUE = ''
    self.OKGREEN = ''
    self.WARNING = ''
    self.FAIL = ''
    self.ENDC = ''
    print '{.HEADER}Hello World{.ENDC}'.format(bcolors, bcolors)
    ```

    This will work on *NIX, MacOS, and Windows (provided you enable [ansi.sys](http://support.microsoft.com/kb/101875)). There are ANSI codes for setting the colour, moving the cursor, and more.

    Libraries
    ---------

    #### [termcolor](http://pypi.python.org/pypi/termcolor)
    E.g.:
    ```
    from termcolor import colored
    print colored('hello', 'red'), colored('world', 'green')
    ```

    - has minimal Windows support

    #### [colorama](http://pypi.python.org/pypi/colorama)
    - has Windows support!

    #### [blessings](http://pypi.python.org/pypi/blessings/)
    - no support for Windows command prompt

    #### [curses](http://docs.python.org/library/curses.html)