Git Product home page Git Product logo

flask-reuploaded's Introduction

CI Status https://coveralls.io/repos/github/jugmac00/flask-reuploaded/badge.svg?branch=master PyPI PyPI - Python Version https://img.shields.io/pypi/l/hibpcli

Flask-Reuploaded

Flask-Reuploaded provides file uploads for Flask.

Notes on this package

This is an independently maintained version of Flask-Uploads based on the 0.2.1 version of the original, but also including four years of unreleased changes, at least not released to PyPi.

Noteworthy is the fix for the Werkzeug API change.

Goals

  • provide a stable drop-in replacement for Flask-Uploads
  • regain momentum for this widely used package
  • provide working PyPI packages

Migration guide from Flask-Uploads

Incompatibilities between Flask-Reuploaded and Flask-Uploads

As already mentioned, staying compatible with Flask-Uploads is one of this project's goals.

Nevertheless, there are the following known incompatibilities:

  • the patch_request_class helper function has been removed; the function was only necessary for Flask 0.6 and earlier. Since then you can use Flask's own MAX_CONTENT_LENGTH environment variable, so you don’t read more than this many bytes from the incoming request data.
  • autoserve of uploaded images now has been deactivated; this was a poorly documented "feature", which even could have lead to unwanted data disclosure; if you want to activate the feature again, you need to set UPLOADS_AUTOSERVE=True

Uninstall and install

If you have used Flask-Uploads and want to migrate to Flask-Reuploaded, you only have to install Flask-Reuploaded instead of Flask-Uploads.

That's all!

So, if you use pip to install your packages, instead of ...

$ pip install `Flask-Uploads`  # don't do this! package is broken

... just do ...

$ pip install `Flask-Reuploaded`

Flask-Reuploaded is a drop-in replacement.

This means you do not have to change a single line of code.

Installation

$ pip install Flask-Reuploaded

Getting started

create an UploadSet

from flask_uploads import IMAGES

photos = UploadSet("photos", IMAGES)

configure your Flask app and this extension

app.config["UPLOADED_PHOTOS_DEST"] = "static/img"
app.config["SECRET_KEY"] = os.urandom(24)
configure_uploads(app, photos)

use photos in your view function

photos.save(request.files['photo'])

See below for a complete example.

Documentation

You can find the documentation at:

https://flask-reuploaded.readthedocs.io/en/latest/

You can generate the documentation locally:

tox -e docs

You can update the dependencies for documentation generation:

tox -e upgradedocs

Minimal example application

Application code, e.g. main.py

import os

from flask import Flask, flash, render_template, request
# please note the import from `flask_uploads` - not `flask_reuploaded`!!
# this is done on purpose to stay compatible with `Flask-Uploads`
from flask_uploads import IMAGES, UploadSet, configure_uploads

app = Flask(__name__)
photos = UploadSet("photos", IMAGES)
app.config["UPLOADED_PHOTOS_DEST"] = "static/img"
app.config["SECRET_KEY"] = os.urandom(24)
configure_uploads(app, photos)


@app.route("/", methods=['GET', 'POST'])
def upload():
    if request.method == 'POST' and 'photo' in request.files:
        photos.save(request.files['photo'])
        flash("Photo saved successfully.")
        return render_template('upload.html')
    return render_template('upload.html')

HTML code for upload.html

<!doctype html>
<html lang=en>
<head>
    <meta charset=utf-8>
    <title>Flask-Reuploaded Example</title>
</head>
<body>
    {% with messages = get_flashed_messages() %}
    {% if messages %}
    <ul class=flashes>
    {% for message in messages %}
        <li>{{ message }}</li>
    {% endfor %}
    </ul>
    {% endif %}
    {% endwith %}

<form method=POST enctype=multipart/form-data action="{{ url_for('upload') }}">
    <input type=file name=photo>
    <button type="submit">Submit</button>
</form>
</body>
</html>

Project structure

The project structure would look as following...

❯ tree -I "__*|h*"
.
├── main.py
├── static
│   └── img
└── templates
    └── upload.html

Running the example application

In order to run the application, you have to enter the following commands...

export FLASK_APP=main.py

❯ flask run

Then point your browser to http://127.0.0.1:5000/.

Contributing

Contributions are more than welcome.

Please have a look at the open issues.

There is also a short contributing guide.

flask-reuploaded's People

Contributors

fdemmer avatar greyli avatar hangim avatar imposeren avatar jugmac00 avatar leafstorm avatar maxcountryman avatar noracodes avatar pkesavap avatar pmlandwehr avatar pre-commit-ci[bot] avatar rduplain avatar shrimpdede avatar trirpi avatar wjt avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.