Skip to main content

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.

Installation

$ pip install flask-download-btn

Quickstart

Our folder structure will look like:

templates/
    index.html
app.py

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

<html>
    <head>
        <!-- include Bootstrap CSS and Javascript -->
        <link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.4.1/css/bootstrap.min.css">
        <script src="https://code.jquery.com/jquery-3.4.1.min.js"></script>
        <script src="https://cdn.jsdelivr.net/npm/popper.js@1.16.0/dist/umd/popper.min.js"></script>
        <script src="https://stackpath.bootstrapcdn.com/bootstrap/4.4.1/js/bootstrap.min.js"></script>
        <!-- include download button script -->
        {{ download_btn.script() }}
    </head>
    <body>
        <!-- render the download button and progress bar -->
        {{ download_btn.btn.render() }}
        {{ download_btn.render_progress() }}
    </body>
</html>

In app.py:

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:'
app.config['SQLALCHEMY_TRACK_MODIFICATIONS'] = False
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
@DownloadBtnManager.register
class DownloadBtn(DownloadBtnMixin, db.Model):
    id = db.Column(db.Integer, primary_key=True)

# create the database and clear the session when the app starts
@app.before_first_request
def before_first_request():
    db.create_all()
    session.clear()

HELLO_WORLD_URL = 'https://test-bucket2357.s3.us-east-2.amazonaws.com/hello_world.txt'

# basic use
@app.route('/')
def index():
    btn = DownloadBtn()
    btn.downloads = [(HELLO_WORLD_URL, 'hello_world.txt')]
    db.session.commit()
    return render_template('index.html', download_btn=btn)

Run the app with:

$ python app.py

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

Citation

@software{bowen2020flask-download-btn,
  author = {Dillon Bowen},
  title = {Flask-Download-Btn},
  url = {https://dsbowen.github.io/flask-download-btn/},
  date = {2020-06-17},
}

License

Users must cite this package in any publications which use it.

It 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.22.tar.gz (10.9 kB view details)

Uploaded Source

Built Distribution

flask_download_btn-0.0.22-py3-none-any.whl (11.9 kB view details)

Uploaded Python 3

File details

Details for the file flask-download-btn-0.0.22.tar.gz.

File metadata

  • Download URL: flask-download-btn-0.0.22.tar.gz
  • Upload date:
  • Size: 10.9 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/2.0.0 pkginfo/1.5.0.1 requests/2.23.0 setuptools/50.3.1 requests-toolbelt/0.9.1 tqdm/4.46.0 CPython/3.6.8

File hashes

Hashes for flask-download-btn-0.0.22.tar.gz
Algorithm Hash digest
SHA256 54e95fcc02ad61e147d995b162e0ed6b62300acd51da7124908d11bd3def2122
MD5 f297f1343d7a4bae71956f5496cbce39
BLAKE2b-256 5b74edfeeccb66c7d3a3cc82c06a1d6647d5d0dbd60d0f092c42f25896b58828

See more details on using hashes here.

File details

Details for the file flask_download_btn-0.0.22-py3-none-any.whl.

File metadata

  • Download URL: flask_download_btn-0.0.22-py3-none-any.whl
  • Upload date:
  • Size: 11.9 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/2.0.0 pkginfo/1.5.0.1 requests/2.23.0 setuptools/50.3.1 requests-toolbelt/0.9.1 tqdm/4.46.0 CPython/3.6.8

File hashes

Hashes for flask_download_btn-0.0.22-py3-none-any.whl
Algorithm Hash digest
SHA256 59eae4e1ae21b700682e343e1c704e9adfad3029043682fe9cc361cebae487ac
MD5 031f44099e46d55336869f1ac6b69c6a
BLAKE2b-256 166a067769cb61e794d0d317c3ad1a8516a42a9dc6c07d8fdc9dbe3065463686

See more details on using hashes here.

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