DZone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Newsletter
Log In / Join
Refcards Trend Reports
Events Video Library
Refcards
Trend Reports

Events

View Events Video Library

Zones

Culture and Methodologies Agile Career Development Methodologies Team Management
Data Engineering AI/ML Big Data Data Databases IoT
Software Design and Architecture Cloud Architecture Containers Integration Microservices Performance Security
Coding Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Partner Zones Build AI Agents That Are Ready for Production
Culture and Methodologies
Agile Career Development Methodologies Team Management
Data Engineering
AI/ML Big Data Data Databases IoT
Software Design and Architecture
Cloud Architecture Containers Integration Microservices Performance Security
Coding
Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance
Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Partner Zones
Build AI Agents That Are Ready for Production

Just dropped: New 2026 “Cloud-Native Foundations” Trend Report. See how teams are tackling complexity, cost & reliability.

AI can investigate. Engineers still decide. See how both work together across incident response in this DZone + Datadog webinar on Oct. 29.

Related

  • Building a Twilio Softphone With JavaScript, HTML, and Flask
  • Stop Poisoning Your Models: How I Built a CV Dataset Quality Toolkit I Can Reuse Forever
  • Beyond Django and Flask: How FastAPI Became Python's Fastest-Growing Framework for Production APIs
  • Build a Simple REST API Using Python Flask and SQLite (With Tests)

Trending

  • A New Chapter for DZone Newsletters
  • Meta Wants to Run Your Business With AI — Microsoft and Salesforce Have a New Rival
  • Detection and Response Did Its Job. Now Someone Has to Actually Fix It.
  • AWS 7R Migration Strategies: A Decision Framework for Engineering Teams
  1. DZone
  2. Coding
  3. Languages
  4. Python: Flask – Generating a Static HTML Page

Python: Flask – Generating a Static HTML Page

Read on for a quick and easy tutorial on how to use Python's flask library to generate an HTML file and create a static page from there.

By 
Mark Needham user avatar
Mark Needham
·
Apr. 30, 17 · Tutorial
Likes (5)
Comment
Save
Tweet
Share
24.7K Views

Join the DZone community and get the full member experience.

Join For Free

Whenever I need to quickly spin up a web application, Python’s Flask library is my go to tool. Recently, however, I found myself wanting to generate a static HTML page to upload to S3 and wondered if I could use it for that as well.

It’s actually not too tricky. If we’re in the scope of the app context then we have access to the template rendering that we’d normally use when serving the response to a web request.

The following code will generate an HTML file based on a template file templates/blog.html:

from flask import render_template
import flask

app = flask.Flask('my app')

if __name__ == "__main__":
    with app.app_context():
        rendered = render_template('blog.html', \
            title = "My Generated Page", \
            people = [{"name": "Mark"}, {"name": "Michael"}])
        print(rendered)

templates/index.html




  
{{ title }}
  
  

{{ title }}

    {% for person in people %}
  • {{ person.name }}
  • {% endfor %}


If we execute the Python script it will generate the following HTML:

$ python blog.py 


  
My Generated Page
  
  

My Generated Page

  • Mark
  • Michael


And we can finish off by redirecting that output into a file:

$ python blog.py  > blog.html

We could also write to the file from Python, but this seems just as easy!

HTML Python (language) Flask (web framework)

Published at DZone with permission of Mark Needham. See the original article here.

Opinions expressed by DZone contributors are their own.

Related

  • Building a Twilio Softphone With JavaScript, HTML, and Flask
  • Stop Poisoning Your Models: How I Built a CV Dataset Quality Toolkit I Can Reuse Forever
  • Beyond Django and Flask: How FastAPI Became Python's Fastest-Growing Framework for Production APIs
  • Build a Simple REST API Using Python Flask and SQLite (With Tests)

Partner Resources

×

Comments

The likes didn't load as expected. Please refresh the page and try again.

  • RSS
  • X
  • Facebook

ABOUT US

  • About DZone
  • Support and feedback
  • Community research

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • Become a Contributor
  • Core Program
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 3343 Perimeter Hill Drive
  • Suite 215
  • Nashville, TN 37211
  • [email protected]

Let's be friends:

  • RSS
  • X
  • Facebook