Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add a new type of radio for single input. Closes #20 #21

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions examples/complex_showcase.py
Original file line number Diff line number Diff line change
@@ -67,6 +67,10 @@ class ShowcaseModel(BaseModel):
single_selection_with_literal: Literal["foo", "bar"] = Field(
"foo", description="Only select a single item from a set."
)
single_selection_with_radio: SelectionValue = Field(
description="Use radio type for single_selection.",
st_kwargs_ui_type="radio",
)
multi_selection: Set[SelectionValue] = Field(
..., description="Allows multiple items from a set."
)
15 changes: 13 additions & 2 deletions examples/union_field.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
from typing import Union

import streamlit as st
from pydantic import BaseModel
from pydantic import BaseModel, Field

import streamlit_pydantic as sp

@@ -17,11 +17,22 @@ class EmailAddress(BaseModel):
send_news: bool


class ContactMethodRadio(BaseModel):
contact: Union[PostalAddress, EmailAddress] = Field(
description="Use radio type for single_selection.",
st_kwargs_ui_type="radio",
)
text: str

st.header("Form radio inputs from model")
input_data = sp.pydantic_input(key="union_radio_input", model=ContactMethodRadio)
if input_data:
st.json(input_data)

class ContactMethod(BaseModel):
contact: Union[PostalAddress, EmailAddress]
text: str


st.header("Form inputs from model")
input_data = sp.pydantic_input(key="union_input", model=ContactMethod)
if input_data:
45 changes: 38 additions & 7 deletions src/streamlit_pydantic/ui_renderer.py
Original file line number Diff line number Diff line change
@@ -57,6 +57,16 @@ def _is_compatible_image(mime_type: str) -> bool:
def _is_compatible_video(mime_type: str) -> bool:
return mime_type in ["video/mp4"]

def _get_current_ui_component(overwrite_kwargs, streamlit_app):
current_ui_attr_name = ""
if "ui_type" in overwrite_kwargs:
if type(overwrite_kwargs["ui_type"]) == str:
current_ui_attr_name = overwrite_kwargs["ui_type"]
del overwrite_kwargs["ui_type"]
if hasattr(streamlit_app, current_ui_attr_name) is not None:
return getattr(streamlit_app, current_ui_attr_name)
st.error(f'no such ui type {current_ui_attr_name}')
return None

class GroupOptionalFieldsStrategy(str, Enum):
NO = "no"
@@ -484,10 +494,19 @@ def _render_single_enum_input(
if len(select_options) == 1:
return select_options[0]
else:
try:
current_component = _get_current_ui_component(overwrite_kwargs, streamlit_app)
return current_component(
**{**streamlit_kwargs, "options": select_options, **overwrite_kwargs}
)
except Exception:
pass

return streamlit_app.selectbox(
**{**streamlit_kwargs, "options": select_options, **overwrite_kwargs}
)


def _render_single_dict_input(
self, streamlit_app: Any, key: str, property: Dict
) -> Any:
@@ -590,13 +609,25 @@ def _render_union_property(
if "help" in streamlit_kwargs:
streamlit_app.markdown(streamlit_kwargs["help"])

selected_reference = streamlit_app.selectbox(
**{
**streamlit_kwargs,
"label": streamlit_kwargs["label"] + " - Options",
"options": name_reference_mapping.keys(),
}
)
overwrite_kwargs = self._get_overwrite_streamlit_kwargs(key, property)
selected_reference = None
try:
current_component = _get_current_ui_component(overwrite_kwargs, streamlit_app)
selected_reference = current_component(
**{
**streamlit_kwargs,
"label": streamlit_kwargs["label"] + " - Options",
"options": name_reference_mapping.keys(),
}
)
except Exception:
selected_reference = streamlit_app.selectbox(
**{
**streamlit_kwargs,
"label": streamlit_kwargs["label"] + " - Options",
"options": name_reference_mapping.keys(),
}
)

input_data = self._render_object_input(
streamlit_app, key, name_reference_mapping[selected_reference]