Flask + Vue.js Web Application Template
- Minimal Flask 1.0 App
- Flask-RestPlus API with class-based secure resource routing
- Starter PyTest test suite
- vue-cli 3 + yarn
- Vuex
- Vue Router
- Axios for backend communication
- Sample Vue Filters
- Heroku Configuration with one-click deployment + Gunicorn
This template was updated on 2018-08-28 to use a flatter folder structure, as well as to use yarn instead of npm.
You can now run yarn serve
as well as other yarn commands from the template root directory.
The old template will continue to live in the npm-template branch
If this setup is not what you are looking for, you might want look at other similar projects:
Prefer Django? Checkout my gtalarico/django-vue-template
The template uses Flask & Flask-RestPlus to create a minimal REST style API, and let's VueJs + vue-cli handle the front end and asset pipline. Data from the python server to the Vue application is passed by making Ajax requests.
The Api is served using a Flask blueprint at /api/
using Flask RestPlus class-based
resource routing.
A simple Flask view is used to serve the entry point into the Vue application at the root endpoint /
The template uses vue-cli 3 and assumes Vue.js & Webpack will manage front-end resources and assets, so it does overwrite template delimiter - it serves static
files as generated by vue cli + webpack
The Vue instance is preconfigured with Filters, Vue-Router, Vuex; each of these can easilly removed if they are not desired.
Location | Content |
---|---|
/app |
Flask Application |
/app/api |
Flask Rest Api (/api ) |
/app/client.py |
Flask Client (/ ) |
/src |
Vue App . |
/src/main.js |
JS Application Entry Point |
/public/index.html |
Html Application Entry Point (/ ) |
/public/static |
Static Assets |
/dist/ |
Bundled Assets Output (generated at yarn build |
Before getting started, you should have the following installed and running:
- Yarn - instructions
- Vue Cli 3 - instructions
- Python 3
- Pipenv (optional)
- Heroku Cli (if deploying to Heroku)
-
Clone this repository:
$ git clone https://github.com/gtalarico/flask-vuejs-template.git
-
Setup virtual environment, install dependencies, and activate it:
$ pipenv install --dev $ pipenv shell
-
Install JS dependencies
$ yarn install
Run Flask Api development server:
$ python run.py
From another tab in the same directory, start the webpack dev server:
$ yarn serve
The Vuejs application will be served from localhost:8080
and the Flask Api
and static files will be served from localhost:5000
.
The dual dev-server setup allows you to take advantage of webpack's development server with hot module replacement.
Proxy config in vue.config.js
is used to route the requests
back to Flask's Api on port 5000.
If you would rather run a single dev server, you can run Flask's
development server only on :5000
, but you have to build build the Vue app first
and the page will not reload on changes.
$ yarn build
$ python run.py
This template is configured to work with Heroku + Gunicorn and it's pre-configured to have Heroku build the application before releasing it.
Heroku's nodejs buidlpack will handle install for all the dependencies from the packages.json
file.
It will then trigger the postinstall
command which calls yarn build
.
This will create the bundled dist
folder which will be served by whitenoise.
- Build your Vue Application:
$ python -m app build
This commands is a shorcut for cd-ing into /app/client/vue_app
and running $ npm run build
.
32f8110eb43778e60c37f5f9a426bb5efe8a8013
The Procfile
will run Django migrations and then launch Django's app using gunicorn, as recommended by heroku.
Here are the commands we need to run to get things setup on the Heroku side:
```
$ heroku apps:create flask-vuejs-template-demo
$ heroku git:remote --app flask-vuejs-template-demo
$ heroku buildpacks:add --index 1 heroku/nodejs
$ heroku buildpacks:add --index 2 heroku/python
$ heroku config:set FLASK_ENV=production
$ heroku config:set FLASK_SECRET=SuperSecretKey
$ git push heroku
```