0

I know that similar questions were asked before, but I couldn't find anything exactly on point. Say I have this list:

tags = ['<div>','<body>','<h1>']

I can easily use f-strings here:

for tag in tags:
   print(f'this is your tag: {tag}')

Output:

this is your tag: <div>
this is your tag: <body>
this is your tag: <h1>

So far so good. But I am really trying to do, is get the same output but with the tag names printed in, for example, red. And this is where I run into problems with the brackets. If I use:

from IPython.display import HTML as html_print

for tag in tags:
     html_print(f'this is your tag: {tag}')

nothing prints out - even if I remove the tags.

I tried:

from IPython.display import Markdown, display

And then first:

for tag in tags:
   display(f'this is your tag: {tag}')

That works like a regular print.

If, however, I try:

for tag in tags:    
   display(Markdown((f'this is your tag: {tag}')))

The output is:

this is your tag:
this is your tag:
this is your tag: 

My understanding is that I need Markdown to print in color, but obviously the brackets are causing problems with the f-strings in Markdown, unlike the case with print and display. So how do I get around that?

Jack Fleeting
  • 24,385
  • 6
  • 23
  • 45
  • https://stackoverflow.com/questions/36267578/how-to-display-a-python-string-as-html-in-jupyter-notebook – hpaulj Apr 18 '19 at 18:41
  • @hpaulj - Nope, doesn't work with tags, I'm afraid. – Jack Fleeting Apr 18 '19 at 19:05
  • What happens if you do `x=HTML(f...)` (or `Markdown`), and then `display(x)`. And look at `x.data`. – hpaulj Apr 18 '19 at 19:14
  • @hpaulj - Tried these too - any combination I could think of; the moment you introduce HTML or Markdown - it's game over. What do you mean by `x.data`? – Jack Fleeting Apr 18 '19 at 19:24
  • Strings like `'
    '` have to be escaped to be displayed in html. Otherwise they are interpreted as html markings. `x = '

    a string

    '` defines an html header. `x='a string

    '` is an incomplete html line.

    – hpaulj Apr 18 '19 at 19:29
  • @hpaulj - Yes, I thought about that, but it requires that I replace each occurrence of `<` in each list item with `<` (and so for all other troublesome characters) before feeding it to `Markdown`. That works (and can be used as answer if all else fails), but I thought there was a simple solution in the IPython library. – Jack Fleeting Apr 18 '19 at 19:41
  • 1
    `import html` and `html.escape(tag)`. – hpaulj Apr 18 '19 at 19:42
  • @hpaulj - You've got it! Now it works. Thanks! Do you want to turn it into an answer? – Jack Fleeting Apr 18 '19 at 19:49

1 Answers1

5

Thanks to @hpaulj (in the comments to the question) we now have a nice and simple answer - add html.escape(tag) to the code. The final lineup looks like this:

from IPython.display import Markdown, display
import html

for tag in tags:    
    tag = html.escape(tag)
    display(Markdown((f'this is your tag: <text style=color:red>{tag}</text>')))

Output:

enter image description here

Simple and effective...

Jack Fleeting
  • 24,385
  • 6
  • 23
  • 45