Skip to content

Commit

Permalink
add mypy static type checking for frontend to pre-commit
Browse files Browse the repository at this point in the history
  • Loading branch information
mt7180 committed Mar 25, 2024
1 parent 5fc8b3d commit 275d829
Show file tree
Hide file tree
Showing 5 changed files with 59 additions and 21 deletions.
12 changes: 11 additions & 1 deletion .pre-commit-config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -11,4 +11,14 @@ repos:
- repo: https://github.com/astral-sh/ruff-pre-commit
rev: v0.3.4
hooks:
- id: ruff
- id: ruff

# mypy static type checker
- repo: https://github.com/pre-commit/mirrors-mypy
rev: v1.8.0
hooks:
- id: mypy
name: mypy-frontend
files: ^frontend/
args: [--config-file=frontend/mypy.ini]
additional_dependencies: [types-requests]
10 changes: 10 additions & 0 deletions frontend/mypy.ini
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
[mypy]
# https://mypy.readthedocs.io/en/stable/config_file.html

python_version = 3.10
disallow_incomplete_defs = True
no_implicit_optional = True
install_types = True
exclude = frontend/utils/request_wrapper.py
ignore_missing_imports = True

36 changes: 19 additions & 17 deletions frontend/streamlit_app.py
Original file line number Diff line number Diff line change
@@ -1,17 +1,22 @@
# run command from root: streamlit run streamlit_app.py
import os
import sys
import logging
import pathlib
import random
from fastapi import UploadFile
from typing import Callable

from streamlit.runtime.uploaded_file_manager import UploadedFile

# from fastapi import UploadFile
import streamlit as st
from streamlit_option_menu import option_menu
from streamlit_extras.add_vertical_space import add_vertical_space
from streamlit_extras.stylable_container import stylable_container
import requests
import os
import sys
import logging
import certifi

from PIL import Image
import certifi
from dotenv import load_dotenv
import sentry_sdk

Expand All @@ -35,7 +40,7 @@
logging.info(f"{API_URL=}")

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

logging.basicConfig(stream=sys.stdout, level=logging.INFO)
Expand Down Expand Up @@ -169,21 +174,18 @@ def display_options_menu():
st.session_state["redirect_page"] = None


def make_get_request(route: str):
def make_get_request(route: str) -> requests.Response:
return requests.get(os.path.join(API_URL, route))


def post_data_to_backend(
route: str, url: str = "", uploaded_file: UploadFile | None = None
):
route: str, url: str = "", uploaded_file: UploadedFile | None = None
) -> None:
with st.spinner("Waiting for openai API response"):
try:
if url:
data = {"upload_url": url}
files = {"upload_file": ("", None)}
response = requests.post(
os.path.join(API_URL, route), data=data, files=files
)
response = requests.post(os.path.join(API_URL, route), data=data)
elif uploaded_file:
files = {"upload_file": (uploaded_file.name, uploaded_file)}
data = {"upload_url": ""}
Expand Down Expand Up @@ -287,7 +289,7 @@ def display_sidemenu():
st.experimental_rerun()


@register_page(MAIN_PAGE)
@register_page(PAGE_REGISTRY_DICT)
def questionai():
with st.container():
for message in st.session_state.messages:
Expand Down Expand Up @@ -335,7 +337,7 @@ def questionai():
)


@register_page(MAIN_PAGE)
@register_page(PAGE_REGISTRY_DICT)
def quizme():
with st.container():
if st.session_state["chat_mode"] == "database":
Expand Down Expand Up @@ -392,7 +394,7 @@ def quizme():
)


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

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


if __name__ == "__main__":
Expand Down
Empty file added frontend/utils/__init__.py
Empty file.
22 changes: 19 additions & 3 deletions frontend/utils/helpers.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,23 @@
def register_page(page_dict: dict[str, callable]) -> callable:
"""decorator to register page automatically in page dict"""
from typing import Callable, TypeVar

F = TypeVar("F", bound=Callable[..., None])


def register_page(page_registry_dict: dict[str, F]) -> Callable[[F], F]:
"""
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
Returns:
Callable: the actual decorator, a callable which takes a callable as
argument and returns it.
"""

def inner(func):
page_dict[func.__name__] = func
page_registry_dict[func.__name__] = func

return inner

0 comments on commit 275d829

Please sign in to comment.