configuration
Configuration
from dara.core.configuration import Configuration
class Configuration(GenericModel)
Definition of the main framework configuration
Attributes
- auth_config: BaseAuthConfig
- registry_lookup: CustomRegistryLookup
- actions:
- endpoint_configurations:
- components:
- context_components:
- enable_devtools: bool
- live_reload: bool
- pages:
- routes:
- scheduled_jobs:
- startup_functions:
- static_folders:
- static_files_dir: str
- package_tag_processors:
- template_extra_js: str
- task_module:
- template: str
- template_renderers:
- theme:
- title: str
- ws_handlers:
- encoders:
- middlewares:
Methods
get_package_map
def get_package_map() -> Dict[str, str]
Get a map of python package names to js package names, based on currently registered components, actions etc.
ConfigurationBuilder
from dara.core.configuration import ConfigurationBuilder
class ConfigurationBuilder()
The ConfigurationBuilder class lets you build a Configuration object up piece by piece using helper methods that aid setup and point users in the right direction.
Attributes
- auth_config: BaseAuthConfig
- registry_lookup: CustomRegistryLookup
- _actions:
- _components:
- _errors:
- enable_devtools: bool
- live_reload: bool
- _pages:
- _template_renderers:
- _endpoint_configurations:
- _static_folders:
- _package_tags_processors:
- _template_extra_js: str
- _custom_ws_handlers:
- _custom_encoders:
- _middlewares:
- routes:
- static_files_dir: str
- scheduled_jobs:
- startup_functions:
- context_components:
- task_module:
- template: str
- theme: BaseTheme
- title: str
Methods
add_action
def add_action(action: Type[ActionImpl], local: bool = False)
Register an Action with the application.
For example the below would explicitly import the UpdateVariable action from dara.core.
add_action(UpdateVariable)
Note that in most cases explicitly registering a action is not necessary in most cases for non-local actionts. Actions are auto-discovered based on imports within your application.
Arguments:
action
: ActionImpl-subclass definitionlocal
: whether the action is a local one. For local actions js_module is not required, as their location is defined via dara.config.json
add_endpoint
def add_endpoint(endpoint: ApiRoute)
Register an API endpoint in the application
add_context_component
def add_context_component(component: ComponentInstance)
Add a ContextComponent to the application.
Context components are rendered recursively in order around the application. E.g.
config.add_context_component(Component1)
config.add_context_component(Component2)
# Results in
<Component1>
<Component2>
<Page>
</Component2>
</Component1>
This can be used by packages outside of core to provide global functionality to components on the page and hook into other application contexts, e.g. the router.
Arguments:
component
: component instance
add_component
def add_component(component: Type[ComponentInstance], local: bool = False)
Register a Component with the application.
For example the below would import the Menu component from dara.core.
add_component(Menu)
Note that in most cases explicitly registering a component is not necessary in most cases for non-local components. Components are auto-discovered based on imports within your application.
Arguments:
component
: ComponentInstance-subclass definitionlocal
: whether the component is a local one. For local components js_module is not required, as their location is defined via dara.config.json
add_configuration
def add_configuration(config: EndpointConfiguration)
Register an EndpointConfiguration instance with the application.
add_static_folder
def add_static_folder(path: str)
Register a static folder, its contents can be then addressed with /static/{asset} URLs.
Its contents will be moved into the local 'static' folder to be served when:
- running the app locally
- running the build script
add_ws_handler
def add_ws_handler(kind: str, handler: Callable[[str, Any], Any])
Register a custom websocket handler, which will be called when a message of a given kind is received.
Example:
# Received message
{
"type": "custom",
"message": {
"kind": "my_custom_kind",
"data": "some arbitrary data"
}
}
# Handler
config.add_ws_handler(kind='my_custom_kind', handler=my_custom_handler)
def my_custom_handler(channel: str, data: Any):
print(f'Received message on channel {channel} with data {data}')
return 'some response'
# Message sent back
{
"type": "custom",
"message": {
"kind": "my_custom_kind",
"data": "some response"
}
}
add_encoder
def add_encoder(typ: Type[EncoderType], serialize: Callable[[EncoderType],
Any],
deserialize: Callable[[Any], EncoderType])
Register a custom encoder, which serialize and deserialize the type
Arguments:
typ
: The type of objectserialize
: The handler to serialize the object to basic python typedeserialize
: The handler to deserialize the object Example: import numpy config.add_encoder(typ=np.array, serialize=lambda x: x.tolist(), deserialize=lambda y: np.array(y))
add_package_tags_processor
def add_package_tags_processor(processor: Callable[[Dict[str, List[str]]],
Dict[str, List[str]]])
Append a package tag processor. This is a function that takes a dictionary of package names to lists of script/link tags included when running in the UMD mode.
All processors are called in order, and the output of each processor is passed to the next one.
add_page
def add_page(name: str,
content: Union[ComponentInstanceType,
Type[CallableClassComponent], str],
icon: Optional[str] = None,
route: Optional[str] = None,
include_in_menu: Optional[bool] = True,
reset_vars_on_load: Optional[List[AnyVariable]] = [],
on_load: Optional[Action] = None)
Add a new page to the layout of the platform. Switching between pages relies on the template implementing a
router for switching between the pages. The default template will do this automatically, but refer to the template documentation for more details on this.
Arguments:
name
: the name of the pagecontent
: the content for the pageicon
: an optional icon for the page, see dara_core.css.get_icon for more detailroute
: an optional url for the page, if not provided it's generated based on the page nameinclude_in_menu
: an optional flag for not including the page in the main menuon_load
: optional action to execute upon visiting the page
add_template_renderer
def add_template_renderer(name: str,
template_renderer: Callable[..., Template]) -> str
Add a new template renderer that can be selected by name as part of the configuration. By default calling this
function will update the template name to match automatically
Arguments:
name
: the template nametemplate_renderer
: the ComponentInstance to use for the template
add_registry_lookup
def add_registry_lookup(registry_lookup: CustomRegistryLookup)
Register custom external registry lookup handlers, which will be called when a uid its not in the server registry
Example:
from dara.core import ConfigurationBuilder
config = ConfigurationBuilder()
def get_derived_variable(uid)-> DerivedVariableRegistryEntry:
# return derived variable
def get_action(uid)-> Callable:
# return action
config.add_registry_lookup(
{
'DerivedVariable': get_derived_variable,
'Action Handler': get_action,
}
)
#### add\_middleware
```python
def add_middleware(middleware: Union[type, Callable], **options: Any)
Add a middleware to the application. The middleware can be a class or a callable.
set_theme
def set_theme(main_theme: Optional[Union[ThemeDef, Literal['light'],
Literal['dark']]] = None,
base_theme: Optional[Union[Literal['light'],
Literal['dark']]] = None)
Sets the color theme of the app. Takes ThemeDef models for the app, and reverts
to the default themes if they are not supplied.
Arguments:
main_theme
: ThemeDef defining colors for the app, alternatively can be either 'light' or 'dark' to use default Dara colors
on_startup
def on_startup(startup_function: Callable)
Adds a function to be run upon startup of a Dara app
Arguments:
startup_function
: The function to be run