Skip to content

Instantly share code, notes, and snippets.

@antonymilne
Created February 13, 2024 09:31
Show Gist options
  • Save antonymilne/174c4c49ace2dae1f1d7674183b8d140 to your computer and use it in GitHub Desktop.
Save antonymilne/174c4c49ace2dae1f1d7674183b8d140 to your computer and use it in GitHub Desktop.
import abc
from vizro.models.types import capture, CapturedCallable
def pure_f(a: int, b: int) -> int:
return a + b
f1 = capture("action")(pure_f)
# Above is same as
# @capture("action")
# def f1(a: int, b: int) -> int:
# return a + b
# calling f1() returns CapturedCallable
# So do f1()(1, 2) or f1(1, 2)() or f1(1)(2) etc. to actually get a + b
class CapturedActionCallable(CapturedCallable, abc.ABC):
def __init__(self, *args, **kwargs):
super().__init__(self.pure_function, *args, **kwargs)
@staticmethod
@abc.abstractmethod
# Maybe need to have self so can access inputs/outputs/components?
# If not then, keep as staticmethod.
# Call it just function, no underscores.
def pure_function(a, b):
return a + b
# Should these also be abstract? Probably not.
# Should they be class properties? Maybe.
# Will you ever need self in them? Maybe.
@property
def inputs(self):
return []
@property
def outputs(self):
return []
@property
def components(self):
# Do we really need this? Should it return an empty list?
return []
# Need @functools.wraps for this to work nicely with docstring etc., but that's not so important
# Will have one class per action
# f2 operates same way as f1, so f2() returns a CapturedCallable
class f2(CapturedActionCallable):
@staticmethod
def pure_function(a, b):
return a + b
@property
def inputs(self):
return ["a", "b"]
# Use an existing action inside a new one
def pure_g(a: int, b: int) -> int:
# Or f2()._function() works
# f2.__wrapped__ doesn't work since don't have functools.wraps
# Maybe make a helper function to make this look less weird
# But no way to just do as f2(1, 2) or wouldn't work when you use that as a standalone action
return 2 * f2(a, b)()
g1 = capture("action")(pure_g)
# Above is same as
# @capture("action")
# def g1(a: int, b: int) -> int:
# return 2 * f2(a, b)()
# Advanced user could do this
class g2(f2):
@staticmethod
def pure_function(a, b):
# Need to do super(g2, g2) or specify f2 explicitly as in staticmethod.
return 2 * f2.pure_function(a, b)
# def inputs(self):
# return ...
# Do this for actions but not tables - for that have separate models and also modes
# Should we alter capture("action") to return CapturedActionCallable?
# How to do:
# Do change for simplest action, review it
# Look again at Action model and how to handle inputs/outputs, whether to move anything from Action into CapturedActionCallable
# Then roll out to all actions
# Try to remove CapturedCallable._function
# Then look at next bits of refactoring e.g. on_page_load - bits might need to change again, but sure that this is step
# in right direction
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment