views:

49

answers:

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,

A: 

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.

Mike Driscoll
A: 

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>
FogleBird