Tethys Component Utilities

Utilities for working with Tethys components.

class tethys_components.utils.DotNotationDict

Wrapper for event args provided by ReactPy as dicts to allow attribute access

class tethys_components.utils.OlManager(attr)

A utility class for dynamically building OpenLayers (OL) configuration dictionaries. This class uses dynamic attribute access to construct nested namespace paths and generate configuration objects for OpenLayers components. It enables a fluent interface for creating OL geometry and feature configurations. .. rubric:: Example

>>> manager = OlManager("ol")
>>> result = manager.geom.Point(coords=[0, 0])
>>> result
{'type': 'ol.geom.Point', 'coords': [0, 0]}
class tethys_components.utils.Props(**kwargs)

Wrapper for ReactPy component property dictionaries that allow them to be passed as python kwargs instead. They are converted back to ReactPy propery dictionaries when accessed.

Example

Instead of lib.html.div({"backgroundColor": "red", "fontSize": "12px"}, "Hello"), you can use lib.html.div(Props(background_color="red", font_size="12px"), "Hello")

class tethys_components.utils.Style(**kwargs)

Wrapper for style dictionaries that allow them to be passed as python kwargs instead. This namely converts dashes ("-") to underscores for styles

Example

Instead of lib.html.div(style={"background-color": "red", "font-size": "12px"}, "Hello"), try using lib.html.div(style=Style(background_color="red, font_size="12px"), "Hello")

tethys_components.utils.args_to_dot_notation_dicts(func)

Wrapper that converts all dict args to AttrDict objects

Return type:

callable

tethys_components.utils.background_execute(func, args=None, delay_seconds=None, repeat_seconds=None, callback=None)

Kick off a task in the background, optionally with a delay

Parameters:
  • func (Callable) -- The function that will be executed on a thread in the background

  • args (list) -- A list of arguments that should be passed to the provided function when executed

  • delay_seconds (int|float) -- The number of seconds after which the provided function should be executed

  • repeat_seconds (int|float) -- Will re-execute the provided function every X seconds

  • callback (Callable) -- A function that will be called when the provided function has completed.

Returns: None

tethys_components.utils.fetch(url)

Fetches data from url and returns result as string

Return type:

str

tethys_components.utils.fetch_json(url, as_attr_dict=True)

Fetches data expected to be JSON from url and returns result as AttrDict

Return type:

dict | DotNotationDict

tethys_components.utils.find_by_tag(element, tag_name)

Recursively finds all elements with a specific tag name.

tethys_components.utils.remove_comments_and_docstrings(source)

Taken directly from https://stackoverflow.com/a/62074206

tethys_components.utils.transform_coordinate(coordinate, src_proj, target_proj)

Transforms a coordinate from a source projection to a target projection using pyproj.

Parameters:
  • coordinate (list) -- A list representing the coordinate to be transformed, in the format [x, y].

  • src_proj (str or dict) -- The source projection, either as a string (e.g. "EPSG:4326") or a dictionary with a "definition" key containing the projection string.

  • target_proj (str or dict) -- The target projection, either as a string (e.g. "EPSG:3857") or a dictionary with a "definition" key containing the projection string.

Returns:

A list representing the transformed coordinate in the target projection, in the format [x, y].

Raises:
  • ValueError -- If src_proj or target_proj are not in the expected formats.

  • tethys_portal.optional_dependencies.MissingOptionalDependency -- If the pyproj library is not installed.

tethys_components.utils.use_media(user=None)

A custom hook used to access the TethysPath representing the app or user's media directory.

Parameters:

user (auth.models.User) -- If provided, get the TethysPath for the user's media directory, rather than the app's.

Returns:

PathsQuery object if the state of the underlying query is "loading" or "error" TethysPath representing either the app's or user's media directory otherwise

tethys_components.utils.use_public()

A custom hook used to access the TethysPath representing the app's public directory.

Returns:

TethysPath representing the app's public directory

tethys_components.utils.use_resources()

A custom hook used to access the TethysPath representing the app's resources directory.

Returns:

TethysPath representing the app's resources directory

tethys_components.utils.use_setting(setting_name)

A custom hook used to access TethysApp settings asynchronously.

Parameters:

setting_name (str) -- The name of the setting to access, as defined in the TethysApp's settings.py file.

Returns:

The value of the requested setting.

tethys_components.utils.use_workspace(user=None)

A custom hook used to access the TethysPath representing the app or user's workspace directory.

Parameters:

user (auth.models.User) -- If provided, get the TethysPath for the user's workspace directory, rather than the app's.

Returns:

PathsQuery object if the state of the underlying query is "loading" or "error" TethysPath representing either the app's or user's workspace directory otherwise