Skip to main content

Flask-Download-Btn defines a SQLALchemy Mixin for creating Bootstrap download buttons in a Flask application.

Project description

Flask-Download-Btn defines a SQLALchemy Mixin for creating Bootstrap download buttons in a Flask application.

Its features include:

  1. Automatic enabling and disabling. A download button is automatically disabled on click and re-enabled on download completion.
  2. CSRF protection. The download button checks for a CSRF authentication token to ensure the client has permission to download the requested file.
  3. Web form handling. Download buttons are responsive to web forms.
  4. Pre-download operations. Download buttons can easily perform operations before files are downloaded, making it easy to create temporary download files.
  5. Progress bar. Update your clients on download progress with server sent events.


$ pip install flask-download-btn


Our folder structure will look like:


In templates/index.html, paste the following Jinja template:

        <!-- include Bootstrap CSS and Javascript -->
        <link rel="stylesheet" href="">
        <script src=""></script>
        <script src=""></script>
        <script src=""></script>
        <!-- include download button script -->
        {{ download_btn.script() }}
        <!-- render the download button and progress bar -->
        {{ download_btn.btn.render() }}
        {{ download_btn.render_progress() }}


from flask_download_btn import DownloadBtnManager, DownloadBtnMixin

from flask import Flask, render_template, session
from flask_sqlalchemy import SQLAlchemy
from sqlalchemy.ext.orderinglist import ordering_list

app = Flask(__name__)
app.config['SECRET_KEY'] = 'secret'
app.config['SQLALCHEMY_DATABASE_URI'] = 'sqlite:///:memory:'
db = SQLAlchemy(app)
# initialize download button manager with application and database
download_btn_manager = DownloadBtnManager(app, db=db)

# create download button model and register it with the manager
class DownloadBtn(DownloadBtnMixin, db.Model):
    id = db.Column(db.Integer, primary_key=True)

# create the database and clear the session when the app starts
def before_first_request():


# basic use
def index():
    btn = DownloadBtn()
    btn.downloads = [(HELLO_WORLD_URL, 'hello_world.txt')]
    return render_template('index.html', download_btn=btn)

Run the app with:

$ python

And navigate to http://localhost:5000/. Click the download button to download a text file with 'Hello, World!'.


  author = {Dillon Bowen},
  title = {Flask-Download-Btn},
  url = {},
  date = {2020-06-17},


Users must cite Docstring-Markdown in any publications which use this software.

Docstring-Markdown is licensed with the MIT License.

Project details

Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

flask-download-btn-0.0.19.tar.gz (10.7 kB view hashes)

Uploaded Source

Built Distribution

flask_download_btn-0.0.19-py3-none-any.whl (11.8 kB view hashes)

Uploaded Python 3

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page