2

I am making a html window in wxpython and want to print it. Before that I need to enter user input (such as his name or such things ) in the html page. How to do that nicely? Thanks in advance,

2 Answers 2

1

Use Jinja2.

Create an HTML template with variables in the places where you need to display user-entered data. Then render the template with a dictionary containing that data.

Here, I'll write you a helper module.

# templates.py
import jinja2 as jinja

def create_env():
    loader = jinja.FileSystemLoader(PATH_TO_YOUR_TEMPLATES)
    env = jinja.Environment(loader=loader)
    return env

env = create_env()

def render(name, context=None):
    context = context or {}
    return env.get_template(name).render(context)

# my_module.py
import templates

data = {
    'first_name': 'John',
    'last_name': 'Smith',
}

html = templates.render('my_template.html', data)
# do something with html string

# my_template.html
<p>Hello, {{ first_name }} {{ last_name }}.  This is a template.</p>
Sign up to request clarification or add additional context in comments.

Comments

0

There are a couple of approaches that come to my mind. If it's like a form letter where only specific parts will be replaced, then you can just should that to the user and have some text controls for them to fill in. Something like this:

Dear #NAME,

Thank you for contacting #COMPANY, blah blah blah

And then have a text control for each of the replaceable parts. The other method would be to use the RichTextCtrl's Save As HTML functionality. See the wxPython Demo application for an example.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.