Search code examples
pythonflaskflask-sqlalchemyjqwidget

Upload Function with Python


I've been trying to use jqxUploadFiles but instead of php I need to use python.

In a code like below;

from flask import request
from flask.ext.uploads import UploadSet, configure_uploads, ALL

def upload_file():

    files = UploadSet('files', ALL)
    app.config['UPLOADED_FILES_DEST'] = '/uploads'
    configure_uploads(app, files)
    filename = files.save(request.files['files'])
    return filename

where app = Flask(__name__)

but in this project there is;

main = Blueprint('main', __name__)

where it used like @main.route('/', methods=['GET', 'POST'])

and there is a create_app function;

def create_app():
    app = Flask(__name__)
    app.config.from_object(config['development'])

    with app.app_context():
        db.app = app
        db.init_app(app)
        db.create_all()

and it is used in manage.py file;

app = create_app()
manager = Manager(app)


def make_shell_context():
    return dict(app=app, db=db)
manager.add_command("shell", Shell(make_context=make_shell_context))


@manager.command
def test():
    """Run the unit tests."""
    import unittest
    tests = unittest.TestLoader().discover('tests')
    unittest.TextTestRunner(verbosity=2).run(tests)


if __name__ == '__main__':
    manager.run()

So I need to use a function like "upload_file()" but I couldn't understand how "app" created by "create_app()" and Blueprint() relates to each other and how can I use it in upload_file().


Solution

  • At first, you had better make a file defined app, and use it from other script file.

    Using this app, you can register Blueprint object.

    The example below is actually a script that worked.

    app.py

    from flask import Flask
    
    app = Flask(__name__)
    

    main.py

    from flask import Blueprint
    from flask_script import Server, Manager, Shell
    from view import top
    from app import app
    
    app.register_blueprint(top.main)
    
    if __name__ == '__main__':
        app.run(host='192.168.33.40') # please change host
    

    view/top.py

    from flask import Blueprint
    main = Blueprint("top", __name__, url_prefix="/")
    
    from flask import request, render_template, redirect
    from flask.ext.uploads import UploadSet, configure_uploads, ALL
    
    from app import app
    
    def upload_file():
    
        files = UploadSet('files', ALL)
        app.config['UPLOADED_FILES_DEST'] = '/tmp' # please change
        configure_uploads(app, files)
        filename = files.save(request.files['files'])
        return filename
    
    @main.route('/', methods=['GET'])
    def index():
        return render_template('top/index.html')
    
    @main.route('upload', methods=['GET', 'POST'])
    def upload():
        upload_file()
        return redirect('/')
    

    templates/top/index.html

    <form method=POST enctype=multipart/form-data action="/upload">
        <input type=file name=files>
        <input type=submit value=upload>
    </form>
    

    If you have any questions please ask anything!