server¶
api¶
- shekels.server.api.api()[source]¶
Route to Shekels API documentation.
- Returns:
Flassger generated API page.
- Return type:
html
- shekels.server.api.get_api()[source]¶
Creates a Blueprint for the Shekels REST API.
- Returns:
API Blueprint.
- Return type:
flask.Blueprint
- shekels.server.api.handle_data_error(error)[source]¶
Handles errors raise by config validation.
- Parameters:
error (DataError) – Config validation error.
- Returns:
DataError response.
- Return type:
Response
- shekels.server.api.handle_json_decode_error(error)[source]¶
Handles JSON decode errors.
- Parameters:
error (JSONDecodeError) – JSON decode error.
- Returns:
JSONDecodeError response.
- Return type:
Response
- shekels.server.api.handle_runtime_error(error)[source]¶
Handles runtime errors.
- Parameters:
error (RuntimeError) – Runtime error.
- Returns:
RuntimeError response.
- Return type:
Response
- shekels.server.api.handle_sql_error(error)[source]¶
Handles SQL errors.
- Parameters:
error (PandaSQLException) – SQL error.
- Returns:
PandaSQLException response.
- Return type:
Response
- shekels.server.api.initialize()[source]¶
Initialize database with given config.
- Raises:
RuntimeError – If config is invalid.
- Returns:
Flask Response instance.
- Return type:
Response
- shekels.server.api.read()[source]¶
Read database.
- Raises:
RuntimeError – If database has not been initilaized.
RuntimeError – If database has not been updated.
- Returns:
Flask Response instance.
- Return type:
Response
app¶
- shekels.server.app.on_config_update(store)[source]¶
Updates config table with config information from store.
- Parameters:
store (dict) – Store data.
- Returns:
Response.
- Return type:
flask.Response
- shekels.server.app.on_datatable_update(store)[source]¶
Updates datatable with read information from store.
- Parameters:
store (dict) – Store data.
- Returns:
Dash DataTable.
- Return type:
DataTable
- shekels.server.app.on_event(*inputs)[source]¶
Update database instance, and updates store with input data.
- Parameters:
inputs (tuple) – Input elements.
- Returns:
Store data.
- Return type:
dict
- shekels.server.app.on_get_tab(tab, store)[source]¶
Serve content for app tabs.
- Parameters:
tab (str) – Name of tab to render.
store (dict) – Store.
- Returns:
Response.
- Return type:
flask.Response
- shekels.server.app.on_plots_update(store)[source]¶
Updates plots with read information from store.
- Parameters:
store (dict) – Store data.
- Returns:
Plots.
- Return type:
list[dcc.Graph]
- shekels.server.app.readiness()[source]¶
Readiness probe for kubernetes.
- Raises:
HealthError – If api is not availiable.
- Return type:
None
- shekels.server.app.run(app, config_path, debug=False, test=False)[source]¶
Runs a given Shekels app.
- Parameters:
Dash – Shekels app.
config_path (str or Path) – Path to configuration JSON.
debug (bool, optional) – Whether debug mode is turned on. Default: False.
test (bool, optional) – Calls app.run_server if False. Default: False.
components¶
- shekels.server.components.get_button(title)[source]¶
Get a html button with a given title.
- Parameters:
title (str) – Title of button.
- Raises:
TypeError – If title is not a string.
- Returns:
Button element.
- Return type:
Button
- shekels.server.components.get_config_tab(config)[source]¶
Get tab element for Shekels config.
- Parameters:
config (dict) – Configuration to be displayed.
- Returns:
List of elements for config tab.
- Return type:
list
- shekels.server.components.get_configbar(config, query='select * from config')[source]¶
Get a row of elements used for configuring Shekels.
- Parameters:
config (dict) – Configuration to be displayed.
query (str, optional) – Query string. Default: None.
- Returns:
Div with buttons and JSON editor.
- Return type:
Div
- shekels.server.components.get_dash_app(server, storage_type='memory')[source]¶
Generate Dash Flask app instance.
- Parameters:
server (Flask) – Flask instance.
storage_type (str) – Storage type (used for testing). Default: memory.
- Returns:
Dash app instance.
- Return type:
Dash
- shekels.server.components.get_data_tab(query=None)[source]¶
Get tab element for Shekels data.
- Parameters:
query (str, optional) – Query string. Default: None.
- Returns:
List of elements for data tab.
- Return type:
list
- shekels.server.components.get_datatable(data, color_scheme={'bg': '#181818', 'blue1': '#5F95DE', 'blue2': '#93B6E6', 'cyan1': '#7EC4CF', 'cyan2': '#B6ECF3', 'dark1': '#040404', 'dark2': '#141414', 'dialog1': '#444459', 'dialog2': '#5D5D7A', 'green1': '#8BD155', 'green2': '#A0D17B', 'grey1': '#242424', 'grey2': '#444444', 'light1': '#A4A4A4', 'light2': '#F4F4F4', 'orange1': '#EB9E58', 'orange2': '#EBB483', 'purple1': '#C98FDE', 'purple2': '#AC92DE', 'red1': '#F77E70', 'red2': '#DE958E', 'yellow1': '#E8EA7E', 'yellow2': '#E9EABE'}, editable=False)[source]¶
Gets a Dash DataTable element using given data. Assumes dict element has all columns of table as keys.
- Parameters:
data (list[dict]) – List of dicts.
color_scheme (dict, optional) – Color scheme dictionary. Default: COLOR_SCHEME.
editable (bool, optional) – Whether table is editable. Default: False.
- Returns:
Table of data.
- Return type:
DataTable
- shekels.server.components.get_dummy_elements()[source]¶
Returns a list of all elements with callbacks so that the client will not throw errors in each tab.
- Returns:
List of html elements.
- Return type:
list
- shekels.server.components.get_key_value_table(data, id_='key-value', header='', editable=False, key_order=None)[source]¶
Gets a Dash DataTable element representing given dictionary.
- Parameters:
data (dict) – Dictionary.
id (str, optional) – CSS id. Default: ‘key-value’.
header (str, optional) – Table header title. Default: ‘’.
editable (bool, optional) – Whether table is editable. Default: False.
key_order (list[str], optional) – Order in which keys will be displayed. Default: None.
- Returns:
Tablular representation of given dictionary.
- Return type:
DataTable
- shekels.server.components.get_plots(data, plots)[source]¶
Gets a Dash plots using given dicts. Assumes dict element has all columns of table as keys.
- Parameters:
data (list[dict]) – List of dicts defining data.
plots (list[dict]) – List of dicts defining plots.
- Raises:
EnforceError – If data is not a list of dicts.
EnforceError – If plots is not a list of dicts.
- Returns:
Plots.
- Return type:
list[dcc.Graph]
event_listener¶
- class shekels.server.event_listener.EventListener(app, store, memory=10)[source]¶
Bases:
object
Listens for Dash app events and calls registered callbacks.
- __init__(app, store, memory=10)[source]¶
Constructs EventListener.
- Parameters:
app (dash.Dash) – Dash application instance.
store (dict) – Dash store.
memory (int, optional) – Number of state changes to remember. Default: 10.
- Raises:
EnforceError – If app is not an instance of dash.Dash.
EnforceError – If app is not an instance of dict.
EnforceError – If memory is less than 1.
- emit(event, value)[source]¶
Call a registered callback guven an event and value.
- Parameters:
event (str) – Event name.
value (object) – Value to be given to callback.
- Raises:
EnforceError – If event is not a string.
- Returns:
self.
- Return type:
- listen(event, callback)[source]¶
Listen for given event and call given callback.
- Parameters:
event (str) – Event name.
callback (function) – Function of form (value, store, app) -> store.
- Raises:
EnforceError – If event is not a string.
- Returns:
self.
- Return type:
- property store¶
Copy of last item in state.
- Type:
dict
server_tools¶
- shekels.server.server_tools.config_edit_event(value, store, app)[source]¶
Saves given edits to store.
- Parameters:
value (dict) – Config table.
store (dict) – Dash store.
app (dash.Dash) – Dash app.
- Returns:
Modified store.
- Return type:
dict
- shekels.server.server_tools.config_query_event(value, store, app)[source]¶
Updates given store given a config query.
- Parameters:
value (str) – SQL query.
store (dict) – Dash store.
app (dash.Dash) – Dash app.
- Returns:
Modified store.
- Return type:
dict
- shekels.server.server_tools.data_query_event(value, store, app)[source]¶
Updates given store given a data query.
- Parameters:
value (str) – SQL query.
store (dict) – Dash store.
app (dash.Dash) – Dash app.
- Returns:
Modified store.
- Return type:
dict
- shekels.server.server_tools.error_to_dict(error)[source]¶
Convenience function for formatting a given exception as a dictionary.
- Parameters:
error (Exception) – Error to be formatted.
- Returns:
Error dictionary.
- Return type:
Dict[str, Any]
- shekels.server.server_tools.error_to_response(error)[source]¶
Convenience function for formatting a given exception as a Flask Response.
- Parameters:
error (Exception) – Error to be formatted.
- Returns:
Flask response.
- Return type:
flask.Response
- shekels.server.server_tools.init_event(value, store, app)[source]¶
Initializes app database.
- Parameters:
value (None) – Ignored.
store (dict) – Dash store.
app (dash.Dash) – Dash app.
- Returns:
Modified store.
- Return type:
dict
- shekels.server.server_tools.parse_json_file_content(raw_content)[source]¶
Parses JSON file content as supplied by HTML request.
- Parameters:
raw_content (str) – Raw JSON file content.
- Raises:
ValueError – If header is invalid.
JSONDecodeError – If JSON is invalid.
- Returns:
JSON content or reponse dict with error.
- Return type:
dict
- shekels.server.server_tools.render_template(filename, parameters, directory=None)[source]¶
Renders a jinja2 template given by filename with given parameters.
- Parameters:
filename (str) – Filename of template.
parameters (dict) – Dictionary of template parameters.
directory (str or Path, optional) – Templates directory. Default: ‘../../../templates’.
- Returns:
HTML.
- Return type:
bytes
- shekels.server.server_tools.save_event(value, store, app)[source]¶
Save store config to app.api.config path.
- Parameters:
value (None) – Ignore me.
store (dict) – Dash store.
app (dash.Dash) – Dash app.
- Returns:
Modified store.
- Return type:
dict
- shekels.server.server_tools.solve_component_state(store, config=False)[source]¶
Solves what component to return given the state of the given store.
Returns a key value table component embedded with a relevant message or error if a required key is not found in the store, or it contain a dictionary with am “error” key in it. Those required keys are as follows:
/config
/config/search
/api/initialize
/api/update
/api/search
- Parameters:
store (dict) – Dash store.
config (bool, optional) – Whether the component is for the config tab. Default: False.
- Returns:
- Key value table if store values are not present or have errors,
otherwise, none.
- Return type:
Div
- shekels.server.server_tools.store_key_is_valid(store, key)[source]¶
Determines if given key is in store and does not have an error.
- Parameters:
store (dict) – Dash store.
key (str) – Store key.
- Raises:
PreventUpdate – If key is not in store.
- Returns:
True if key exists and does not have an error key.
- Return type:
bool
- shekels.server.server_tools.update_event(value, store, app)[source]¶
Update app database.
- Parameters:
value (None) – Ignored.
store (dict) – Dash store.
app (dash.Dash) – Dash app.
- Returns:
Modified store.
- Return type:
dict
- shekels.server.server_tools.update_store(client, store, endpoint, data=None)[source]¶
Updates store with data from given endpoint. Makes a post call to endpoint with client.
- Parameters:
client (FlaskClient) – Flask client instance.
store (dict) – Dash store.
endpoint (str) – API endpoint.
data (dict, optional) – Data to be provided to endpoint request.