CtrlK
BlogDocsLog inGet started
Tessl Logo

tessl/pypi-flask-admin

Simple and extensible admin interface framework for Flask

86

1.30x

Quality

Pending

Does it follow best practices?

Impact

86%

1.30x

Average score across 10 eval scenarios

Overview
Eval results
Files

geoalchemy-integration.mddocs/

GeoAlchemy Integration

Geographic data support for SQLAlchemy models through GeoAlchemy2 integration. Provides interactive map-based form fields and geographic data formatting for spatial database administration.

Capabilities

Geographic Model View

Enhanced SQLAlchemy model view with geographic data support and interactive map widgets.

from flask_admin.contrib.geoa import ModelView

class ModelView(SQLAModelView):
    """
    GeoAlchemy2-enabled model view for geographic data administration.
    
    Attributes:
    - model_form_converter: AdminModelConverter for geographic field conversion
    - column_type_formatters: Geographic data formatters
    - tile_layer_url: Optional custom tile layer URL for maps
    - tile_layer_attribution: Attribution text for map tiles
    """
    
    model_form_converter = AdminModelConverter
    column_type_formatters = DEFAULT_FORMATTERS
    tile_layer_url = None
    tile_layer_attribution = None

GeoJSON Form Field

Interactive map-based form field for editing geographic data with coordinate system transformations.

from flask_admin.contrib.geoa.fields import GeoJSONField

class GeoJSONField(JSONField):
    def __init__(
        self,
        label=None,
        validators=None,
        geometry_type="GEOMETRY",
        srid='-1',
        session=None,
        tile_layer_url=None,
        tile_layer_attribution=None,
        **kwargs
    ):
        """
        Interactive geographic data field with map widget.
        
        Parameters:
        - label: str, Field label
        - validators: list, Form validators
        - geometry_type: str, Geographic geometry type (POINT, POLYGON, etc.)
        - srid: str, Spatial Reference System Identifier
        - session: SQLAlchemy session for coordinate transformations
        - tile_layer_url: str, Custom map tile layer URL
        - tile_layer_attribution: str, Map attribution text
        """

Leaflet Map Widget

Interactive Leaflet-based map widget for geographic data visualization and editing.

from flask_admin.contrib.geoa.widgets import LeafletWidget

class LeafletWidget(TextArea):
    def __init__(
        self,
        width='auto',
        height=350,
        center=None,
        zoom=None,
        min_zoom=None,
        max_zoom=None,
        max_bounds=None,
        tile_layer_url=None,
        tile_layer_attribution=None
    ):
        """
        Leaflet map widget for geographic data editing.
        
        Parameters:
        - width: Map width (auto or pixel value)
        - height: int, Map height in pixels
        - center: tuple, Map center coordinates (lat, lng)
        - zoom: int, Initial zoom level
        - min_zoom: int, Minimum zoom level
        - max_zoom: int, Maximum zoom level
        - max_bounds: Bounding box for map limits
        - tile_layer_url: str, Custom tile server URL
        - tile_layer_attribution: str, Attribution text
        """

Form Converter

Automatic conversion of GeoAlchemy2 geographic column types to appropriate form fields.

from flask_admin.contrib.geoa.form import AdminModelConverter

class AdminModelConverter(SQLAAdminConverter):
    @converts('Geography', 'Geometry')
    def convert_geom(self, column, field_args, **extra):
        """
        Convert GeoAlchemy2 geographic columns to GeoJSON form fields.
        
        Parameters:
        - column: GeoAlchemy2 column definition
        - field_args: dict, Field configuration arguments
        - extra: Additional conversion parameters
        
        Returns:
        GeoJSONField: Configured geographic form field
        """

Geographic Data Formatting

Type formatters for displaying geographic data in list and detail views.

from flask_admin.contrib.geoa.typefmt import DEFAULT_FORMATTERS

# Geographic data type formatters
DEFAULT_FORMATTERS = {
    # Automatic formatting for GeoAlchemy2 geometric types
    # Converts geometric data to human-readable formats
}

Usage Examples

Basic Geographic Model View

from flask import Flask
from flask_admin import Admin
from flask_admin.contrib.geoa import ModelView
from flask_sqlalchemy import SQLAlchemy
from geoalchemy2 import Geometry

app = Flask(__name__)
db = SQLAlchemy(app)
admin = Admin(app)

# Geographic model
class Location(db.Model):
    id = db.Column(db.Integer, primary_key=True)
    name = db.Column(db.String(100))
    coordinates = db.Column(Geometry('POINT'))

# Add geographic model view
admin.add_view(ModelView(Location, db.session, name='Locations'))

Custom Map Configuration

class LocationView(ModelView):
    # Custom tile layer (e.g., OpenStreetMap)
    tile_layer_url = 'https://{s}.tile.openstreetmap.org/{z}/{x}/{y}.png'
    tile_layer_attribution = '© OpenStreetMap contributors'
    
    # Form field customization
    form_overrides = {
        'coordinates': GeoJSONField
    }
    
    form_args = {
        'coordinates': {
            'geometry_type': 'POINT',
            'srid': '4326',  # WGS84
            'tile_layer_url': tile_layer_url,
            'tile_layer_attribution': tile_layer_attribution
        }
    }

admin.add_view(LocationView(Location, db.session))

Advanced Geographic Features

class AdvancedGeoView(ModelView):
    # Custom map widget configuration
    form_widget_args = {
        'boundary': {
            'data-geometry-type': 'POLYGON',
            'data-zoom': 10,
            'data-lat': 40.7128,
            'data-lng': -74.0060,
            'data-min-zoom': 5,
            'data-max-zoom': 18
        }
    }
    
    # Geographic column formatting
    column_formatters = {
        'coordinates': lambda v, c, m, p: f"Point({m.coordinates.x}, {m.coordinates.y})" if m.coordinates else "No location"
    }

# Model with multiple geographic columns
class Region(db.Model):
    id = db.Column(db.Integer, primary_key=True)
    name = db.Column(db.String(100))
    center_point = db.Column(Geometry('POINT'))
    boundary = db.Column(Geometry('POLYGON'))

admin.add_view(AdvancedGeoView(Region, db.session, name='Regions'))

Requirements

The GeoAlchemy integration requires additional dependencies:

pip install geoalchemy2 shapely

These packages provide:

  • GeoAlchemy2: SQLAlchemy extension for geographic data
  • Shapely: Geometric object manipulation and analysis

Install with Tessl CLI

npx tessl i tessl/pypi-flask-admin

docs

appengine-integration.md

core-admin.md

file-admin.md

forms.md

geoalchemy-integration.md

helpers-utilities.md

index.md

model-views.md

mongoengine-integration.md

redis-integration.md

sqlalchemy-integration.md

tile.json