Skip to content

Commit

Permalink
Merge pull request #58 from mt7180/clean-page-register-decorator
Browse files Browse the repository at this point in the history
cleaning page register approach with decorator class
  • Loading branch information
mt7180 authored May 14, 2024
2 parents de6beec + b8595a9 commit 0545cf0
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 22 deletions.
12 changes: 5 additions & 7 deletions frontend/streamlit_app.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
import logging
import pathlib
import random
from typing import Callable

from streamlit.runtime.uploaded_file_manager import UploadedFile

Expand All @@ -18,7 +17,7 @@
from dotenv import load_dotenv
import sentry_sdk

from utils.helpers import register_page
from utils.helpers import MultiPage


# workaround for mac to solve "SSL: CERTIFICATE_VERIFY_FAILED Error"
Expand All @@ -38,7 +37,6 @@
logging.info(f"{API_URL=}")

APP_TITLE = "Quaigle"
PAGE_REGISTRY_DICT: dict[str, Callable[..., None]] = {}
cfd = pathlib.Path(__file__).parent

logging.basicConfig(stream=sys.stdout, level=logging.INFO)
Expand Down Expand Up @@ -285,7 +283,7 @@ def display_sidemenu():
st.rerun()


@register_page(PAGE_REGISTRY_DICT)
@MultiPage
def questionai():
with st.container():
messages = stylable_container(
Expand Down Expand Up @@ -347,7 +345,7 @@ def questionai():
)


@register_page(PAGE_REGISTRY_DICT)
@MultiPage
def quizme():
if st.session_state["chat_mode"] == "database":
st.markdown(
Expand Down Expand Up @@ -403,7 +401,7 @@ def quizme():
)


@register_page(PAGE_REGISTRY_DICT)
@MultiPage
def statistics():
import pandas as pd

Expand Down Expand Up @@ -446,7 +444,7 @@ def main():
display_header()
display_sidemenu()
# implement the selected page from options menu
PAGE_REGISTRY_DICT[st.session_state.selected_page]()
MultiPage.registry[st.session_state.selected_page]()


if __name__ == "__main__":
Expand Down
27 changes: 12 additions & 15 deletions frontend/utils/helpers.py
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)

0 comments on commit 0545cf0

Please sign in to comment.