commadelimited
commadelimited

Reputation: 5119

Setting up Python Flask app with admin section

I've built a few small Flask apps before that were read only and I really enjoyed the process. now I'm adding an admin section to a Flask app and I'm looking for some guidance.

Current directory structure looks like this:

├── Makefile
├── Procfile
├── app.py
├── requirements.txt
├── static
│   ├── css
│   ├── fonts
│   ├── img
│   └── js
└── templates
    ├── about.html
    ├── base.html
    ├── contact.html
    └── index.html

My app.py file looks like this:

import os
from flask import Flask, render_template

app = Flask(__name__)
app.debug = True


# MAIN APPLICATION
@app.route('/')
@app.route('/work/<gallery>/')
def index(gallery='home'):
    return render_template('index.html', gallery=gallery)


@app.route('/views/<view>/')
def view(view):
    return render_template(view + '.html')


@app.route('/data/<gallery>/<size>/')
def data(gallery='home', size='md'):
    data = '[\
        {"image": "/static/img/photos/md/img_1.jpg","color": "white"},\
        {"image": "/static/img/photos/md/img_2.jpg","color": "white"},\
        {"image": "/static/img/photos/md/img_3.jpg","color": "black"}\
        ]'
    return data


if __name__ == '__main__':
    # Bind to PORT if defined, otherwise default to 5000.
    port = int(os.environ.get('PORT', 5000))
    app.run(host='0.0.0.0', port=port)

I've done some research and found the Blueprints framework and Flask-Admin which seem like they might work together. Does anyone have an alternate suggestion that might be more efficient or easier to set up?

Upvotes: 0

Views: 1508

Answers (1)

Joes
Joes

Reputation: 1818

Blueprints allow you to group functionality into isolated modules.

If you feel your service will be very small, there's no need to use blueprints - you can add routes to the Flask application, like you do in your example.

However, if application will get bigger, it is better to split it into smaller pieces by using blueprints. In your example, "work" is first possible blueprint, "views" is another, etc.

Flask-Admin allows you to build administrative interface a-la Django admin. It does not matter how you're structuring rest of your application - you add Flask-Admin and it just works.

Upvotes: 1

Related Questions