Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add option to specify deployed fuctions as public #645

Merged
merged 6 commits into from
Sep 10, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions tabpy/tabpy_server/handlers/management_handler.py
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,7 @@ def _add_or_update_endpoint(self, action, name, version, request_data):
target = request_data.get("target", None)
schema = request_data.get("schema", None)
src_path = request_data.get("src_path", None)
is_public = request_data.get("is_public", None)
target_path = get_query_object_path(
self.settings[SettingsParameters.StateFilePath], name, version
)
Expand Down Expand Up @@ -117,6 +118,7 @@ def _add_or_update_endpoint(self, action, name, version, request_data):
dependencies=dependencies,
target=target,
schema=schema,
is_public=is_public,
)
else:
self.tabpy_state.update_endpoint(
Expand All @@ -129,6 +131,7 @@ def _add_or_update_endpoint(self, action, name, version, request_data):
target=target,
schema=schema,
version=version,
is_public=is_public,
)

except Exception as e:
Expand Down
12 changes: 12 additions & 0 deletions tabpy/tabpy_server/management/state.py
Original file line number Diff line number Diff line change
Expand Up @@ -187,6 +187,12 @@ def _check_and_set_dependencies(self, dependencies, defaultValue):

return dependencies

def _check_and_set_is_public(self, is_public, defaultValue):
if is_public is None:
return defaultValue

return is_public

@state_lock
def add_endpoint(
self,
Expand All @@ -198,6 +204,7 @@ def add_endpoint(
target=None,
dependencies=None,
schema=None,
is_public=None,
):
"""
Add a new endpoint to the TabPy.
Expand Down Expand Up @@ -231,6 +238,7 @@ def add_endpoint(
docstring, "-- no docstring found in query function --")
endpoint_type = self._check_and_set_endpoint_type(endpoint_type, None)
dependencies = self._check_and_set_dependencies(dependencies, [])
is_public = self._check_and_set_is_public(is_public, False)

self._check_target(target)
if target and target not in endpoints:
Expand All @@ -246,6 +254,7 @@ def add_endpoint(
"creation_time": int(time()),
"last_modified_time": int(time()),
"schema": schema,
"is_public": is_public,
}

endpoints[name] = endpoint_info
Expand Down Expand Up @@ -289,6 +298,7 @@ def update_endpoint(
target=None,
dependencies=None,
schema=None,
is_public=None,
):
"""
Update an existing endpoint on the TabPy.
Expand Down Expand Up @@ -330,6 +340,7 @@ def update_endpoint(
endpoint_type, endpoint_info["type"])
dependencies = self._check_and_set_dependencies(
dependencies, endpoint_info.get("dependencies", []))
is_public = self._check_and_set_is_public(is_public, endpoint_info["is_public"])

self._check_target(target)
if target and target not in endpoints:
Expand All @@ -352,6 +363,7 @@ def update_endpoint(
"creation_time": endpoint_info["creation_time"],
"last_modified_time": int(time()),
"schema": schema,
"is_public": is_public,
}

endpoints[name] = endpoint_info
Expand Down
23 changes: 18 additions & 5 deletions tabpy/tabpy_tools/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -173,7 +173,8 @@ def get_endpoints(self, type=None):
"dependencies": [],
"last_modified_time": 1469511182,
"type": "model",
"target": null},
"target": null,
"is_public": True}
"add": {
"description": "",
"docstring": "-- no docstring found in query function --",
Expand All @@ -182,7 +183,8 @@ def get_endpoints(self, type=None):
"dependencies": [],
"last_modified_time": 1469505967,
"type": "model",
"target": null}
"target": null,
"is_public": False}
}
"""
return self._service.get_endpoints(type)
Expand All @@ -191,7 +193,7 @@ def _get_endpoint_upload_destination(self):
"""Returns the endpoint upload destination."""
return self._service.get_endpoint_upload_destination()["path"]

def deploy(self, name, obj, description="", schema=None, override=False):
def deploy(self, name, obj, description="", schema=None, override=False, is_public=False):
"""Deploys a Python function as an endpoint in the server.

Parameters
Expand Down Expand Up @@ -220,6 +222,12 @@ def deploy(self, name, obj, description="", schema=None, override=False):
RuntimeError. If True and there is already an endpoint with that
name, it will deploy a new version on top of it.

is_public : bool, optional
Whether a function should be public for viewing from within tableau. If
False, function will not appear in the custom functions explorer within
Tableau. If True, function will be visible ta anyone on a site with this
analytics extension configured

See Also
--------
remove, get_endpoints
Expand All @@ -236,7 +244,7 @@ def deploy(self, name, obj, description="", schema=None, override=False):

version = endpoint.version + 1

obj = self._gen_endpoint(name, obj, description, version, schema)
obj = self._gen_endpoint(name, obj, description, version, schema, is_public)

self._upload_endpoint(obj)

Expand All @@ -256,7 +264,7 @@ def remove(self, name):
Endpoint name to remove'''
self._service.remove_endpoint(name)

def _gen_endpoint(self, name, obj, description, version=1, schema=None):
def _gen_endpoint(self, name, obj, description, version=1, schema=None, is_public=False):
"""Generates an endpoint dict.

Parameters
Expand All @@ -274,6 +282,10 @@ def _gen_endpoint(self, name, obj, description, version=1, schema=None):
version : int
The version. Defaults to 1.

is_public : bool
True if function should be visible in the custom functions explorer
within Tableau

Returns
-------
dict
Expand Down Expand Up @@ -318,6 +330,7 @@ def _gen_endpoint(self, name, obj, description, version=1, schema=None):
"required_files": [],
"required_packages": [],
"schema": copy.copy(schema),
"is_public": is_public,
}

def _upload_endpoint(self, obj):
Expand Down
2 changes: 2 additions & 0 deletions tabpy/tabpy_tools/rest_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ class Endpoint(RESTObject):
evaluator = RESTProperty(str)
schema_version = RESTProperty(int)
schema = RESTProperty(str)
is_public = RESTProperty(bool)

def __new__(cls, **kwargs):
"""Dispatch to the appropriate class."""
Expand All @@ -67,6 +68,7 @@ def __eq__(self, other):
and self.evaluator == other.evaluator
and self.schema_version == other.schema_version
and self.schema == other.schema
and self.is_public == other.is_public
)


Expand Down
Loading