7.2.3.1. Base¶
- class Base(*args, **kwargs)[source]¶
Base class for dispatcher objects.
Methods
Creates a dispatcher Form Flask app.
Returns a sub node of a dispatcher.
Plots the Dispatcher with a graph in the DOT language with Graphviz.
Creates a dispatcher Flask app.
- __init__()¶
- web(depth=-1, node_data=none, node_function=none, directory=None, sites=None, run=True, subsite_idle_timeout=600)[source]¶
Creates a dispatcher Flask app.
- Parameters:
depth (int, optional) – Depth of sub-dispatch API. If negative all levels are configured.
node_data (tuple[str], optional) – Data node attributes to produce API.
node_function (tuple[str], optional) – Function node attributes produce API.
directory (str, optional) – Where is the generated Flask app root located?
sites (set[Site], optional) – A set of
Site
to maintain alive the backend server.run (bool, optional) – Run the backend server?
subsite_idle_timeout (int, optional) – Idle timeout of a debug subsite in seconds.
- Returns:
A WebMap.
- Return type:
Example:
From a dispatcher like this:
>>> from schedula import Dispatcher >>> dsp = Dispatcher(name='Dispatcher') >>> def fun(a): ... return a + 1, a - 1 >>> dsp.add_function('fun', fun, ['a'], ['b', 'c']) 'fun'
You can create a web server with the following steps:
>>> print("Starting...\n"); site = dsp.web(); site Starting... Site(WebMap([(Dispatcher, WebMap())]), host='localhost', ...) >>> import requests >>> url = '%s/%s/%s' % (site.url, dsp.name, fun.__name__) >>> requests.post(url, json={'args': (0,)}).json()['return'] [1, -1] >>> site.shutdown() # Remember to shutdown the server. True
Note
When
Site
is garbage collected, the server is shutdown automatically.
- form(depth=1, node_data=none, node_function=none, directory=None, sites=None, run=True, view=True, get_context=none, get_data=none, subsite_idle_timeout=600, basic_app_config=None, stripe_event_handler=<function Base.<lambda>>)[source]¶
Creates a dispatcher Form Flask app.
- Parameters:
depth (int, optional) – Depth of sub-dispatch API. If negative all levels are configured.
node_data (tuple[str], optional) – Data node attributes to produce API.
node_function (tuple[str], optional) – Function node attributes produce API.
directory (str, optional) – Where is the generated Flask app root located?
sites (set[Site], optional) – A set of
Site
to maintain alive the backend server.run (bool, optional) – Run the backend server?
view (bool, optional) – Open the url site with the sys default opener.
get_context (function | dict, optional) – Function to pass extra data as form context.
get_data (function | dict, optional) – Function to initialize the formdata.
subsite_idle_timeout (int, optional) – Idle timeout of a debug subsite in seconds.
basic_app_config (object, optional) – Flask app config object.
stripe_event_handler (function, optional) – Stripe event handler function.
- Returns:
A FormMap or a Site if sites is None and run or view is True.
- Return type:
- plot(workflow=None, view=True, depth=-1, name=none, comment=none, format=none, engine=none, encoding=none, graph_attr=none, node_attr=none, edge_attr=none, body=none, raw_body=none, node_styles=none, node_data=none, node_function=none, edge_data=none, max_lines=none, max_width=none, directory=None, sites=None, index=True, viz=False, short_name=None, executor='async', render=False, run=False)[source]¶
Plots the Dispatcher with a graph in the DOT language with Graphviz.
- Parameters:
workflow (bool, optional) – If True the latest solution will be plotted, otherwise the dmap.
view (bool, optional) – Open the rendered directed graph in the DOT language with the sys default opener.
node_data (tuple[str], optional) – Data node attributes to view.
node_function (tuple[str], optional) – Function node attributes to view.
node_styles (dict[str|Token, dict[str, str]]) – Default node styles according to graphviz node attributes.
depth (int, optional) – Depth of sub-dispatch plots. If negative all levels are plotted.
name (str) – Graph name used in the source code.
comment (str) – Comment added to the first line of the source.
directory (str, optional) – (Sub)directory for source saving and rendering.
format (str, optional) – Rendering output format (‘pdf’, ‘png’, …).
engine (str, optional) – Layout command used (‘dot’, ‘neato’, …).
encoding (str, optional) – Encoding for saving the source.
graph_attr (dict, optional) – Dict of (attribute, value) pairs for the graph.
node_attr (dict, optional) – Dict of (attribute, value) pairs set for all nodes.
edge_attr (dict, optional) – Dict of (attribute, value) pairs set for all edges.
body (dict, optional) – Dict of (attribute, value) pairs to add to the graph body.
raw_body (list, optional) – List of command to add to the graph body.
directory – Where is the generated Flask app root located?
sites (set[Site], optional) – A set of
Site
to maintain alive the backend server.index (bool, optional) – Add the site index as first page?
max_lines (int, optional) – Maximum number of lines for rendering node attributes.
max_width (int, optional) – Maximum number of characters in a line to render node attributes.
view – Open the main page of the site?
render (bool, optional) – Render all pages statically?
viz (bool, optional) – Use viz.js as back-end?
short_name (int, optional) – Maximum length of the filename, if set name is hashed and reduced.
executor (str, optional) – Pool executor to render object.
run (bool, optional) – Run the backend server?
- Returns:
A SiteMap or a Site if .
- Return type:
Example:
>>> from schedula import Dispatcher >>> dsp = Dispatcher(name='Dispatcher') >>> def fun(a): ... return a + 1, a - 1 >>> dsp.add_function('fun', fun, ['a'], ['b', 'c']) 'fun' >>> dsp.plot(view=False, graph_attr={'ratio': '1'}) SiteMap([(Dispatcher, SiteMap())])
- get_node(*node_ids, node_attr=none)[source]¶
Returns a sub node of a dispatcher.
- Parameters:
node_ids (str) – A sequence of node ids or a single node id. The id order identifies a dispatcher sub-level.
node_attr (str, None, optional) –
Output node attr.
If the searched node does not have this attribute, all its attributes are returned.
When ‘auto’, returns the “default” attributes of the searched node, which are:
for data node: its output, and if not exists, all its attributes.
for function and sub-dispatcher nodes: the ‘function’ attribute.
When ‘description’, returns the “description” of the searched node, searching also in function or sub-dispatcher input/output description.
When ‘output’, returns the data node output.
When ‘default_value’, returns the data node default value.
When ‘value_type’, returns the data node value’s type.
When None, returns the node attributes.
- Returns:
Node attributes and its real path.
- Return type:
(T, (str, …))
Example:
Get the sub node output:
>>> dsp.get_node('Sub-dispatcher', 'c') (4, ('Sub-dispatcher', 'c')) >>> dsp.get_node('Sub-dispatcher', 'c', node_attr='type') ('data', ('Sub-dispatcher', 'c'))
>>> sub_dsp, sub_dsp_id = dsp.get_node('Sub-dispatcher')