Python Developer Guide
Other documentationsDemoCommunityGitHub
  • Forest Admin
  • Getting started
    • How it works
    • Quick start
      • Flask
      • Django
    • Create your agent
    • Troubleshooting
    • Migrating legacy agents
      • Pre-requisites
      • Recommendations
      • Migration steps
      • Code transformations
        • API Charts
        • Live Queries
        • Smart Charts
        • Route overrides
        • Smart Actions
        • Smart Fields
        • Smart Relationships
        • Smart Segments
  • Data Sources
    • Getting Started
      • Collection selection
      • Naming conflicts
      • Query interface and Native Queries
        • Fields and projections
        • Filters
        • Aggregations
    • Provided data sources
      • SQLAlchemy
      • Django
        • Polymorphic relationships
    • Write your own
      • Translation strategy
        • Structure declaration
        • Capabilities declaration
        • Read implementation
        • Write implementation
        • Intra-data source Relationships
      • Contribute
  • Agent customization
    • Getting Started
    • Actions
      • Scope and context
      • Result builder
      • Static Forms
      • Widgets in Forms
      • Dynamic Forms
      • Form layout customization
      • Related data invalidation
    • Charts
      • Value
      • Objective
      • Percentage
      • Distribution
      • Leaderboard
      • Time-based
    • Fields
      • Add fields
      • Move, rename and remove fields
      • Override binary field mode
      • Override writing behavior
      • Override filtering behavior
      • Override sorting behavior
      • Validation
    • Hooks
      • Collection hook
      • Collection override
    • Pagination
    • Plugins
      • Write your own
    • Relationships
      • To a single record
      • To multiple records
      • Computed foreign keys
      • Under the hood
    • Search
    • Segments
  • Frontend customization
    • Smart Charts
      • Create a table chart
      • Create a bar chart
      • Create a cohort chart
      • Create a density map
    • Smart Views
      • Create a Map view
      • Create a Calendar view
      • Create a Shipping view
      • Create a Gallery view
      • Create a custom tinder-like validation view
      • Create a custom moderation view
  • Deploying to production
    • Environments
      • Deploy on AWS
      • Deploy on Heroku
      • Deploy on GCP
      • Deploy on Ubuntu
    • Development workflow
    • Using branches
    • Deploying your changes
    • Forest Admin CLI commands
      • init
      • login
      • branch
      • switch
      • set-origin
      • push
      • environments:create
      • environments:reset
      • deploy
  • Under the hood
    • .forestadmin-schema.json
    • Data Model
      • Typing
      • Relationships
    • Security & Privacy
Powered by GitBook
On this page
  • What can I connect to?
  • Example

Was this helpful?

  1. Data Sources

Getting Started

This is the official documentation of the forestadmin-agent-django and forestadmin-agent-flask Python agents.

After doing the quick start, you should have a development project which is up and running and connected to your main data storage system.

However, you can plug as many data sources as you want into the same agent.

What can I connect to?

Forest Admin collections map to any of those concepts:

  • ORM models

  • Endpoints on SaaS providers (by writing a custom data source)

  • Endpoints on your own API (by writing a custom data source)

Example

Flask / SQLAlchemy

In this example, we import collections from SQLAlchemy ORM.

from flask import Flask
from sqlalchemy.orm import declarative_base
from forestadmin.flask_agent.agent import create_agent
from forestadmin.datasource_sqlalchemy.datasource import SqlAlchemyDatasource

app = Flask(__name__)
Base = declarative_base(engine)

# ...

agent = create_agent(app)
# db_uri parameter is optional, but if the agent can't find
# an engine in your Base class you have to set it
agent.add_datasource(SqlAlchemyDatasource(Base, db_uri='sqlite:///path/to/db.sql'))
from flask_sqlalchemy import SQLAlchemy
from flask import Flask
from forestadmin.flask_agent.agent import create_agent
from forestadmin.datasource_sqlalchemy.datasource import SqlAlchemyDatasource

app = Flask(__name__)
db = SQLAlchemy()

# ...

agent = create_agent(app)
# with app_context you don't have to specify the db_uri parameter
with app.app_context():
    # db.init_app(app)
    agent.add_datasource(SqlAlchemyDatasource(db))

Django

With the Django agent, the Django datasource is automatically added. If you prefer to add the datasource manually you have to:

  • In project/settings.py set FOREST_AUTO_ADD_DJANGO_DATASOURCE = False to not auto add it

  • Add a customization function

  • Reference the customization function in project/settings.py

  • Add the datasource in the customization function FOREST_CUSTOMIZE_FUNCTION="path.to.custom.function"

FOREST_AUTO_ADD_DJANGO_DATASOURCE = False
FOREST_CUSTOMIZE_FUNCTION = "my_app.forest_admin.customize_forest"
# you can also directly pass a function
from my_app.forest_admin import customize_forest
FOREST_CUSTOMIZE_FUNCTION = customize_forest
from forestadmin.django_agent.agent import DjangoAgent
from forestadmin.datasource_django.datasource import DjangoDatasource

def customize_forest(agent: DjangoAgent):
    agent.add_datasource(DjangoDatasource())
PreviousSmart SegmentsNextCollection selection

Last updated 1 year ago

Was this helpful?