-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
cleaning page register approach with decorator class
- Loading branch information
Showing
2 changed files
with
17 additions
and
22 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,24 +1,21 @@ | ||
from typing import TypeVar | ||
import functools | ||
from typing import TypeVar, Generic | ||
from collections.abc import Callable | ||
|
||
F = TypeVar("F", bound=Callable[..., None]) | ||
|
||
|
||
def register_page(page_registry_dict: dict[str, F]) -> Callable[[F], F]: | ||
class MultiPage(Generic[F]): | ||
"""decorator to register multiple streamlit pages (functions) | ||
in a registry dict | ||
""" | ||
Streamlit page decorator (factory) which takes the page registry dict | ||
as argument and returns the actual decorator, which itself registers | ||
all decorated page functions automatically in page dict. | ||
|
||
Args: | ||
page_registry_dict: the dict where to register the page | ||
registry: dict[str, F] = {} | ||
|
||
Returns: | ||
Callable: the actual decorator, a callable which takes a callable as | ||
argument and returns it. | ||
""" | ||
|
||
def inner(func): | ||
page_registry_dict[func.__name__] = func | ||
def __init__(self, func: F): | ||
self.func = func | ||
functools.update_wrapper(self, func) | ||
self.registry[func.__name__] = func | ||
|
||
return inner | ||
def __call__(self, *args, **kwargs): | ||
return self.func(*args, **kwargs) |