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:
- Automatic enabling and disabling. A download button is automatically disabled on click and re-enabled on download completion.
- CSRF protection. The download button checks for a CSRF authentication token to ensure the client has permission to download the requested file.
- Web form handling. Download buttons are responsive to web forms.
- Pre-download operations. Download buttons can easily perform operations before files are downloaded, making it easy to create temporary download files.
- 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 Docstring-Markdown in any publications which use this software.
Docstring-Markdown is licensed with the MIT License.
Project details
Release history Release notifications | RSS feed
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)
Built Distribution
Close
Hashes for flask-download-btn-0.0.19.tar.gz
Algorithm | Hash digest | |
---|---|---|
SHA256 | c752245a725b8a56b7d4264afa1da131f3445cdb0df60df081347ead1a3bad2b |
|
MD5 | 05e0e162db4f8cfb7d322da5aa93ee04 |
|
BLAKE2b-256 | c43341363b7a1c94af59c9496475d7c0fa31d6fb4da04d34a67e5e673e3e0cc9 |
Close
Hashes for flask_download_btn-0.0.19-py3-none-any.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 523d81d0a7014d68895f9ee9d0eaafa98b79a6c74ea0b4f03a81246f3296b9ee |
|
MD5 | bbf03e4c99121c719bcedc91eaa28ce4 |
|
BLAKE2b-256 | 9023f3ba8512d7006259ca41390ae1bb6d9910d8e0ad51f93624861e17b94e01 |