Search code examples
htmlpyqtjinja2line-breaksqtextedit

Insert multiple lines from QTextEdit into Jinja2 template


I have a QTextEdit that has several lines (Name, Age, Height).

self.textbox = QTextEdit(self)
self.textbox.setPlainText('Name:\n Age: \nHeight: \n')
self.enteredText = self.textbox.toPlainText()

I want to put this information into a Jinja2 HTML template so it shows in the following way:

Personal data:
Name: (whatever the user wrote after "name")
Age: (whatever the user wrote after "age")
Height: (whatever the user wrote after "height")

I'm having issues with linebreaks, I'm not sure how to process my QTextEdit so that I also get the linebreaks in HTML. I tried self.enteredText.splitlines(True) but HTML does not understand \n as linebreak and my text is all in one line.


Solution

  • I know almost nothing about jinja html templates, but you could try joining the lines together using br tags:

    lines = '<br/>'.join(self.enteredText.splitlines()).