Display matched patterns in Custom html template

I've created a custom html template. It highlights some text. That works. Now I also want to show the pattern matched text. How can I do that in my custom recipe?

Here is my current recipe code -

    def custom_textcat_teach(dataset, spacy_model, source, patterns, label=None):
    components = teach(dataset=dataset, spacy_model=spacy_model,
                       source=source, patterns=patterns, label=label)
    with open('/home/ec2-user/anaconda3/lib/python3.7/site-packages/prodigy/extension/template.html', 'r') as f:
        template = f.read()
    components['config']['html_template'] = template
    components['view_id'] = 'html'
    return components

Hi! I'm not 100% sure I understand the question – could you share your HTML template?

Here is my html template -
Subject: {{subject}} From: {{from}} To: {{to}}
{{body}}
If there is any pattern matched in the content - subject or body, I'd like they be highlighted as what textcat.teach with pattern does.

Ah, well, in that case you'd actually have to implement the rendering logic for that yourself – slice the text on each match character offsets and then wrap the matched slices in some HTML tag. The algorithm for that isn't complicated and you can see an example of this in the displacy code – but not sure if it's worth it?

Instead of doing it all in HTML, it might be easier to just use a custom interface with blocks and use Prodigy's built-in ner interface that can already render highlighted spans in the text. So your first block would be your HTML block with the html_template and the second block would be the ner block that'd render the "text" and a list of "spans" containing the matches.

Yes, blocks worked! Because my text is a bit long so the result is not prefect since user needs to scroll down to see the better formatted content.
I implemented a classification + html view. Here is my configuration -

with open('/home/ec2-user/anaconda3/lib/python3.7/site- 
packages/prodigy/extension/template.html', 'r') as f:
      template = f.read()

"config": {
                  "lang": nlp.lang,
                   "blocks": [
                        {"view_id": "classification"},
                        {"view_id": "html", "html_template": template},
                   ]
              }
     }

Yay, glad it worked :slightly_smiling_face:

I guess you could leave out the {{body}} in your HTML template and only show the formatted text in the block? (Or maybe I'm misunderstanding the problem.) You could also experiment with some custom theme settings like font size, maximum card width etc. to fit more content on the screen.

That's exactly what I did. I put the context information in html at the top and the classification view at the bottom. The result is not perfect. But it's good for mow. I will play with other css settings. Thanks.