Commit 6f628968 authored by Petrus J.v.Rensburg's avatar Petrus J.v.Rensburg

Add a basic GeoAlchemy example.

parent 45b8a953
SQLAlchemy model backend integration examples.
To run this example:
1. Clone the repository::
git clone https://github.com/flask-admin/flask-admin.git
cd flask-admin
2. Create and activate a virtual environment::
virtualenv env
source env/bin/activate
3. Install requirements::
pip install -r 'examples/geo-alchemy/requirements.txt'
4. Setup the database::
psql postgres
CREATE DATABASE flask_admin_geo;
CREATE ROLE flask_admin_geo LOGIN PASSWORD 'flask_admin_geo';
GRANT ALL PRIVILEGES ON DATABASE flask_admin_geo TO flask_admin_geo;
\q
psql flask_admin_geo
CREATE EXTENSION postgis;
\q
5. Run the application::
python examples/sqla/app.py
6. You will notice that the maps are not rendered. To see them, you will have
to register for a free account at `Mapbox <https://www.mapbox.com/>`_ and set
the *MAPBOX_MAP_ID* and *MAPBOX_ACCESS_TOKEN* config variables accordingly.
\ No newline at end of file
from flask import Flask
from flask_sqlalchemy import SQLAlchemy
import flask_admin as admin
from geoalchemy2.types import Geometry
from flask_admin.contrib.geoa import ModelView
# Create application
app = Flask(__name__)
# Create dummy secrey key so we can use sessions
app.config['SECRET_KEY'] = '123456790'
app.config['SQLALCHEMY_DATABASE_URI'] = 'postgresql+psycopg2://flask_admin_geo:flask_admin_geo@localhost/flask_admin_geo'
app.config['SQLALCHEMY_ECHO'] = True
db = SQLAlchemy(app)
app.config['MAPBOX_MAP_ID'] = "..."
app.config['MAPBOX_ACCESS_TOKEN'] = "..."
class Location(db.Model):
id = db.Column(db.Integer, primary_key=True)
name = db.Column(db.String(64), unique=True)
point = db.Column(Geometry("POINT"))
# Flask views
@app.route('/')
def index():
return '<a href="/admin/">Click me to get to Admin!</a>'
# Create admin
admin = admin.Admin(app, name='Example: GeoAlchemy')
# Add views
admin.add_view(ModelView(Location, db.session))
if __name__ == '__main__':
db.create_all()
# Start app
app.run(debug=True)
Flask
Flask-Admin
Flask-SQLAlchemy
shapely
geoalchemy2
psycopg2
\ No newline at end of file
{% extends 'admin/master.html' %}
{% block body %}
{{ super() }}
<div class="row">
<div class="span10 offset1">
<h1>Flask-Admin example</h1>
<p class="lead">
GeoAlchemy model view.
</p>
<p>
This example shows how to manage spatial information in a GIS database.
</p>
<a class="btn btn-primary" href="/"><i class="icon-arrow-left icon-white"></i> Back</a>
</div>
</div>
{% endblock body %}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment