from yattag import Doc import webbrowser doc, tag, text = Doc().tagtext() doc.asis('<!DOCTYPE html>') with tag('html') as html: with tag('head') as head: with tag('title') as title: text('This is test web page') with tag('body') as body: with tag('h2') as h2: text('This is h2') page = doc.getvalue() f = open('page.html', 'w') f.write(page) f.close() webbrowser.open('page.html')