|
| 1 | +import os |
| 2 | +import streamlit as st |
| 3 | +import streamlit.components.v1 as components |
| 4 | +from typing import Type, Dict, Optional, Tuple |
| 5 | +from pydantic import BaseModel |
| 6 | + |
| 7 | +__all__ = [ |
| 8 | + 'jsonschema_form', |
| 9 | + 'pydantic_form', |
| 10 | + 'pydantic_instance_form', |
| 11 | +] |
| 12 | + |
| 13 | +_RELEASE = True |
| 14 | + |
| 15 | +COMPONENT_NAME = "streamlit_react_jsonschema" |
| 16 | + |
| 17 | +if not _RELEASE: |
| 18 | + _component_func = components.declare_component( |
| 19 | + COMPONENT_NAME, |
| 20 | + url="http://localhost:3001", |
| 21 | + ) |
| 22 | +else: |
| 23 | + parent_dir = os.path.dirname(os.path.abspath(__file__)) |
| 24 | + build_dir = os.path.join(parent_dir, "frontend/build") |
| 25 | + _component_func = components.declare_component(COMPONENT_NAME, path=build_dir) |
| 26 | + |
| 27 | + |
| 28 | +def pydantic_form( |
| 29 | + model: Type[BaseModel], |
| 30 | + *, |
| 31 | + key: str = None, |
| 32 | + default: Dict = None, |
| 33 | +) -> Tuple[Optional[BaseModel], bool]: |
| 34 | + """ |
| 35 | + render a react-json-schema form by json schema from pydantic model |
| 36 | + default ui is material ui v5 |
| 37 | + see details in frontend/ |
| 38 | + :param model: the pydantic model type |
| 39 | + :param key: the elementId of the Form |
| 40 | + :param default: default value of the model. |
| 41 | + :return: (model instance: BaseModel, submitted: bool) |
| 42 | + model instance is None unless the form is submitted, get the pydantic model instance. |
| 43 | + this function use pydantic model to validate the result that form returns. |
| 44 | + """ |
| 45 | + if key is None: |
| 46 | + key = _pydantic_model_key(model) |
| 47 | + schema = model.model_json_schema() |
| 48 | + result, submitted = jsonschema_form(key, schema, default=default) |
| 49 | + if result is not None: |
| 50 | + return model(**result), submitted is True |
| 51 | + return result, submitted is True |
| 52 | + |
| 53 | + |
| 54 | +def _pydantic_model_key(model: Type[BaseModel]) -> str: |
| 55 | + return f"{model.__module__}:{model.__qualname__}" |
| 56 | + |
| 57 | + |
| 58 | +def pydantic_instance_form( |
| 59 | + instance: BaseModel, |
| 60 | + *, |
| 61 | + key: str = None, |
| 62 | + deep: bool = True, |
| 63 | +) -> Tuple[Optional[BaseModel], bool]: |
| 64 | + """ |
| 65 | + render a react-json-schema form by json schema from pydantic instance |
| 66 | + default ui is material ui v5 |
| 67 | + :param instance: pydantic model instance |
| 68 | + :param key: the elementId of the Form |
| 69 | + :param deep: if deep is True, return instance's deep copy by updated values |
| 70 | + :return: (instance, submitted) |
| 71 | + instance is None unless the form is submitted, get the pydantic model instance. |
| 72 | + """ |
| 73 | + data = instance.model_dump(exclude_defaults=False) |
| 74 | + schema = instance.model_json_schema() |
| 75 | + model = type(instance) |
| 76 | + if key is None: |
| 77 | + key = _pydantic_model_key(model) |
| 78 | + result, submitted = jsonschema_form(key, schema, default=data) |
| 79 | + if result is None: |
| 80 | + return None, submitted is True |
| 81 | + return instance.model_copy(update=result, deep=deep), submitted is True |
| 82 | + |
| 83 | + |
| 84 | +def jsonschema_form( |
| 85 | + key: str, |
| 86 | + schema: Dict, |
| 87 | + *, |
| 88 | + default: Dict = None, |
| 89 | +) -> Tuple[Optional[Dict], bool]: |
| 90 | + """ |
| 91 | + render a react-json-schema form by raw json schema |
| 92 | + :param key: the elementId of the Form |
| 93 | + :param schema: the json schema |
| 94 | + :param default: default value of the schema |
| 95 | + :return: None unless the form is submitted, get the dict value of the formData |
| 96 | + """ |
| 97 | + if default is None: |
| 98 | + default = {} |
| 99 | + with st.container(border=True): |
| 100 | + component_value = _component_func( |
| 101 | + key=key, |
| 102 | + schema=schema, |
| 103 | + formData=default, |
| 104 | + ) |
| 105 | + if isinstance(component_value, dict): |
| 106 | + return component_value.get("formData", {}), component_value.get("submitted", False) |
| 107 | + return None, False |
0 commit comments