Skip to content
Open
Show file tree
Hide file tree
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
6 changes: 5 additions & 1 deletion examples/render_output.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import datetime
from typing import Optional

from pydantic import BaseModel, Field

Expand All @@ -9,7 +10,10 @@ class ExampleModel(BaseModel):
text: str = Field(..., description="A text property")
integer: int = Field(..., description="An integer property.")
date: datetime.date = Field(..., description="A date.")
optional_datetime: Optional[datetime.datetime] = Field(...,
description="An optional datetime property."
)


instance = ExampleModel(text="Some text", integer=40, date=datetime.date.today())
instance = ExampleModel(text="Some text", integer=40, date=datetime.date.today(), optional_datetime=datetime.datetime.now())
sp.pydantic_output(instance)
19 changes: 19 additions & 0 deletions src/streamlit_pydantic/schema_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,25 @@
def resolve_reference(reference: str, references: Dict) -> Dict:
return references[reference.split("/")[-1]]

def filter_nullable(property: Dict) -> Dict:
# if it is optional and nullable, it may be
# - {'anyOf': [{'type': '<type>'}, {'type': 'null'}]
# - {'anyOf': [{'$ref': '<ref>'}, {'type': 'null'}]
# we want to remove the "null" type
union_prop = property.get("oneOf", property.get("anyOf"))
if union_prop is not None:
# Remove the null type, while keeping the `$ref` and other types
where = [i for i,d in enumerate(union_prop) if d.get('type') == "null"]
where.reverse()
for i in where:
del union_prop[i]
if len(union_prop) == 1: # it is fine, we wrap the type to the original object
for key in union_prop[0]:
property[key] = union_prop[0][key]

del property["anyOf"] # now we can delete the key

return property

def get_single_reference_item(property: Dict, references: Dict) -> Dict:
# Ref can either be directly in the properties or the first element of allOf
Expand Down
3 changes: 3 additions & 0 deletions src/streamlit_pydantic/ui_renderer.py
Original file line number Diff line number Diff line change
Expand Up @@ -1065,6 +1065,8 @@ def _render_list_input(self, streamlit_app: Any, key: str, property: Dict) -> An
return object_list

def _render_property(self, streamlit_app: Any, key: str, property: Dict) -> Any:
# filter the case of optional and nullable
property = schema_utils.filter_nullable(property)
if schema_utils.is_single_enum_property(property, self._schema_references):
return self._render_single_enum_input(streamlit_app, key, property)

Expand Down Expand Up @@ -1241,6 +1243,7 @@ def _render_single_output(self, streamlit: Any, output_data: BaseModel) -> None:
continue

if property_schema:
property_schema = schema_utils.filter_nullable(property_schema)
if schema_utils.is_multi_file_property(property_schema):
for file in output_property_value:
self._render_single_file_property(
Expand Down