Server#
The server module has two main components: the BaseDracoRouter abstract base class to provide a template for declaring groups of API endpoints which might use a Draco instance under the hood and the DracoAPI class to handle registering the actual endpoints to a FastAPI instance and to allow for customization of the API.
You can learn more on applications of the server module in the applications section which includes examples.
BaseDracoRouter#
- class draco.server.BaseDracoRouter(draco, **kwargs)#
Abstract
APIRouterdeclaring a group of endpoints which might make use of an underlyingDracoinstance, injectable via the constructor.- __init__(draco, **kwargs)#
- Parameters:
draco (
Draco) – the underlyingDracoinstance to be used by the router endpoints.kwargs – keyword arguments to be passed to the
APIRouterconstructor.
- register()#
Convenience method to register the endpoints of the router by calling the
_registermethod.
Note
As of now, this abstract class has four concrete implementations:
ClingoRouter#
Note
Defines endpoints under /clingo.
- class draco.server.routers.ClingoRouter(draco, **kwargs)#
Bases:
BaseDracoRouterRouter exposing Clingo functionality through REST endpoints.
- __init__(draco, **kwargs)#
- Parameters:
draco – the underlying
Dracoinstance to be used by the router endpoints.kwargs – keyword arguments to be passed to the
APIRouterconstructor.
DracoRouter#
Note
Defines endpoints under /draco.
- class draco.server.routers.DracoRouter(draco, **kwargs)#
Bases:
BaseDracoRouterRouter exposing core functionality of
Dracothrough REST endpoints.- __init__(draco, **kwargs)#
- Parameters:
draco – the underlying
Dracoinstance to be used by the router endpoints.kwargs – keyword arguments to be passed to the
APIRouterconstructor.
RendererRouter#
Note
Defines endpoints under /renderer.
- class draco.server.routers.RendererRouter(draco, **kwargs)#
Bases:
BaseDracoRouterRouter exposing renderer functionality through REST endpoints. To spare the round trip between the client and the server, the actual dataset to be rendered does not need to be sent to the server. The returned specification will contain an empty dataset and the client is responsible for filling it in.
- __init__(draco, **kwargs)#
- Parameters:
draco – the underlying
Dracoinstance to be used by the router endpoints.kwargs – keyword arguments to be passed to the
APIRouterconstructor.
UtilityRouter#
Note
Defines endpoints under /utility.
- class draco.server.routers.UtilityRouter(draco, **kwargs)#
Bases:
BaseDracoRouterRouter exposing utility functionality through REST endpoints.
- __init__(draco, **kwargs)#
- Parameters:
draco – the underlying
Dracoinstance to be used by the router endpoints.kwargs – keyword arguments to be passed to the
APIRouterconstructor.
DracoAPI#
- class draco.server.DracoAPI(draco=Draco(), app=FastAPI(), base_routers=None)#
A class responsible for conveniently creating a FastAPI server exposing the capabilities of Draco. Provides sensible defaults while allowing for customization of the underlying FastAPI instance as well as the routers used to expose the Draco features in a granular way.
- __init__(draco=Draco(), app=FastAPI(), base_routers=None)#
Creates a new
DracoAPIinstance. If no parameters are passed in, the default configuration ofDracoandFastAPIis used.The
base_routersparameter governs the routers which get registered to theFastAPIinstance, that is, they define the actual endpoints which will be available. Default routers get registered automatically:DracoRouter- exposes the core functionality ofDracoClingoRouter- exposes the functionality of the Clingo solverRendererRouter- exposes renderer functionalityUtilityRouter- exposes various utility endpoints
If you wish to enable only a subset of these endpoints, you can pass in a list of routers to the
base_routersparameter. The routers will be registered in the order they appear in the list. Note that to avoid inconsistencies between theDracoinstance and the routers, you should declare aDracoinstance first, use it to construct your routers, then pass it into the constructor too.Let’s suppose that you want to enable only
DracoRouterandUtilityRouterand configure them to use custom prefix and tags:>>> from draco import Draco >>> from draco.server.routers import DracoRouter, UtilityRouter >>> from draco.server import DracoAPI >>> draco = Draco() >>> draco_router = DracoRouter(draco, >>> prefix='/my-draco', >>> tags=['My Draco Tag']) >>> utility_router = UtilityRouter(draco, >>> prefix='/my-utility', >>> tags=['My Utility Tag']) >>> my_base_routers = [draco_router, utility_router] >>> my_api = DracoAPI(draco=draco, base_routers=my_base_routers)
You can use the very same method to add support for more endpoints by registering further routers.
- Parameters:
draco (
Draco) – TheDracoinstance to use. Use this instance to configure the Draco programs such as constraints and weights.app (
FastAPI) – TheFastAPIinstance to use. Use this instance to configure the FastAPI server according to your needs.base_routers (
list[BaseDracoRouter] |None) – Routers to be used by the server, defining the actual endpoints.