Is it possible to print using different colors in ipython's Notebook?

后端 未结 12 1158
孤独总比滥情好
孤独总比滥情好 2020-12-23 09:15

Is it somehow possible to have certain output appear in a different color in the IPython Notebook? For example, something along the lines of:

 print(\"Hello          


        
12条回答
  •  旧时难觅i
    2020-12-23 09:46

    Using @Evert answer, here is a method that would wrap a list of tokens in red and return a highlighted string

    def color_in_tokens(tokens, color_token_contains="_"):
      """
      Highlights the tokens which contain 'color_token_contains'
    
      :param tokens: list of strings
      :param color_token_contains: str (the string for marking a token red)
      :return: str
      """
      return " ".join(["\x1b[31m%s\x1b[0m" % i if color_token_contains in i else i for i in tokens])
    

    Just call print on the returned string:

提交回复
热议问题