1

Example:

@register.simple_tag
def last_orders(user):
    rows = []
    for order in Order.objects.filter(user=user).order_by('-offer__date'):
        rows.append(format_html('<tr><td>{}</td><td>{}</td><td>{} Eur</td><td class="int col-1">{}</td><td>{} Eur</td></div>',
                                order.offer.date, order.offer.text, order.offer.price, order.amount, order.amount*order.offer.price))
    if not rows:
        return format_html('<div>No orders up to now.</div>')
    return format_html('''Last Orders: 
     <table class="table table-striped">
      <tr><th>Date</th><th>Text</th><th>Price</th><th class="col-1">Amount</th><th>Sum</th></tr>
      {}
     </table>
     ''', join(rows))

Is PyCharm able to do HTML Syntax checking and highlighting in Python strings like above?

bad_coder
  • 11,289
  • 20
  • 44
  • 72
guettli
  • 25,042
  • 81
  • 346
  • 663

2 Answers2

3

Yes! PyCharm can do both...

To have both functionalities in a Python string containing HTML first requires using an IDE functionality called "Language injections".

In the context of the IDE "Syntax checks" are usually called "Inspections" and "Syntax highlighting" is under "Color Scheme", as follows (compare with the Python entries in the same sections):

Functionalities IDE path to dialogue
Syntax checks Settings > Editor > Inspections > HTML
Syntax highlights Settings > Editor > Color Sheme > HTML

Using Language Injections:

The best guide I've found on the JetBrains site is: "Language injections (for IntelliJ IDEA)", it follows the exact same steps as the guide for the PyCharm IDE but is more elaborate. (I also liked this youtube video that shows how to do "Language injections" for several different languages.)

I used the following Python code separating the string with a backslash \ because it's easier for seeing syntax error warnings on separate lines:

# language=HTML prefix=<body> suffix=<body>
your_python_string = '\
    <body text=""><h1>some header text</h1></body>\
    t<br></br> \
    <dir>\
    '

Having said that, lets configure the functionalities:

Start by clicking inside the Python string literal and press Alt + Enter or click in light bulb pop-up; choose "Inject language or reference" and next choose "HTML" as show below, the background color of the string literal should change:

image of configuring language injections

Syntax checks (or Inspections) to the HTML:

At this point if you press Alt + 6 (running Inspect Code gives additional warnings) there should be a docked window with the HTML warnings and syntax check. You will also have HTML code hints available:

image of warnings and code hints working

Syntax highlights:

This dialogue is integrated into the PyCharm per language, after having activated "Language injection" for the string literal it already has HTML specific highlight, you can adjust the color scheme for better contrast to your preference:

adjusting syntax highlight

A final thought:

The warnings that are emitted by PyCharm for HTML are shown in the screenshot below. Other functionalities associated with HTML files are also available in the HTML injected string.

image of HTML inspections

bad_coder
  • 11,289
  • 20
  • 44
  • 72
0

@guettli As far as I know, PyCharm will just see embedded HTML code as string because of the ' ' used around the HTML code.

HeroicHitesh
  • 77
  • 1
  • 8