Skip to content
14 changes: 14 additions & 0 deletions tesseract_streamlit/parse.py
Original file line number Diff line number Diff line change
Expand Up @@ -273,6 +273,9 @@ class _InputField(typing.TypedDict):
description: str
ancestors: list[str]
default: NotRequired[typing.Any]
minimum: NotRequired[float | int]
maximum: NotRequired[float | int]
multiple_of: NotRequired[float | int]
Comment thread
jpbrodrick89 marked this conversation as resolved.
Resolved


def _key_to_title(key: str) -> str:
Expand Down Expand Up @@ -305,19 +308,27 @@ def _format_field(
description=field_data.get("description", None),
ancestors=[*ancestors, field_key],
)

if "properties" not in field_data: # signals a Python primitive type
if field["type"] != "object":
default_val = field_data.get("default", None)
if (field_data["type"] == "string") and (default_val is None):
default_val = ""
field["default"] = default_val
if field_data["type"] in ("number", "integer"):
field["minimum"] = field_data.get("minimum", None)
field["maximum"] = field_data.get("maximum", None)
field["multiple_of"] = field_data.get("multipleOf", None)
Comment thread
jacanchaplais marked this conversation as resolved.
Resolved
return field
field["title"] = _key_to_title(field_key) if use_title else field_key
if ARRAY_PROPS <= set(field_data["properties"]):
data_type = "array"
if _is_scalar(field_data["properties"]["shape"]):
data_type = "number"
field["default"] = field_data.get("default", None)
field["minimum"] = field_data.get("minimum", None)
field["maximum"] = field_data.get("maximum", None)
field["multiple_of"] = field_data.get("multipleOf", None)
field["type"] = data_type
return field
# at this point, not an array or primitive, so must be composite
Expand Down Expand Up @@ -404,6 +415,9 @@ class JinjaField(typing.TypedDict):
description: str
title: str
default: NotRequired[typing.Any]
minimum: NotRequired[float | int]
maximum: NotRequired[float | int]
multiple_of: NotRequired[float | int]


def _input_to_jinja(field: _InputField) -> JinjaField:
Expand Down
9 changes: 8 additions & 1 deletion tesseract_streamlit/templates/template.j2
Original file line number Diff line number Diff line change
Expand Up @@ -84,14 +84,21 @@ st.markdown({% if not udf.docs %}""{% else %}
{% elif field.type == 'integer' %}
{{ field.uid }} = {{ field.container }}.number_input(
"{{ field.get('title', field.uid) }}",
step=1,
min_value={{ field.get("minimum", None) }},
max_value={{ field.get("maximum", None) }},
step={{ field.get("multiple_of", 1) }},
format="%d",
key="int.{{ field.key }}",
value=_cast_type({{ field.get("default", None) }}, int),
)
{% elif field.type == 'number' %}
{{ field.uid }} = {{ field.container }}.number_input(
"{{ field.get('title', field.uid) }}",
min_value={{ field.get("minimum", None) }},
max_value={{ field.get("maximum", None) }},
{% if field.get('multiple_of') is not none %}
Comment thread
jpbrodrick89 marked this conversation as resolved.
Resolved
step={{ field.multiple_of }},
{% endif %}
Comment thread
jpbrodrick89 marked this conversation as resolved.
Resolved
key="number.{{ field.key }}",
format="%f",
value=_cast_type({{ field.get("default", None) }}, float),
Expand Down
106 changes: 105 additions & 1 deletion tests/mock-schema-fields.json
Comment thread
jacanchaplais marked this conversation as resolved.
Resolved
Original file line number Diff line number Diff line change
@@ -1 +1,105 @@
[{"type":"string","title":"Name","description":"Name of the person you want to greet.","ancestors":["name"],"default":"John Doe"},{"type":"integer","title":"Age","description":"Age of person in years.","ancestors":["age"],"default":30},{"type":"number","title":"Height","description":"Height of person in cm.","ancestors":["height"],"default":175.0},{"type":"boolean","title":"Alive","description":"Whether the person is (currently) alive.","ancestors":["alive"],"default":true},{"type":"number","title":"Weight","description":"The person's weight in kg.","ancestors":["weight"],"default":null},{"type":"array","title":"Leg Lengths","description":"The length of the person's left and right legs in cm.","ancestors":["leg_lengths"]},{"type":"composite","title":"Hobby","description":"The person's only hobby.","ancestors":["hobby"]},{"type":"string","title":"Name","description":"Name of the activity.","ancestors":["hobby","name"],"default":""},{"type":"boolean","title":"Active","description":"Does the person actively engage with it?","ancestors":["hobby","active"],"default":null},{"type":"integer","title":"Experience","description":"Experience practising it in years.","ancestors":["hobby","experience"],"default":null}]
[
{
"type": "string",
"title": "Name",
"description": "Name of the person you want to greet.",
"ancestors": [
"name"
],
"default": "John Doe"
},
{
"type": "integer",
"title": "Age",
"description": "Age of person in years.",
"ancestors": [
"age"
],
"default": 30,
"maximum": 125,
"minimum": 0,
"multiple_of": null
},
{
"type": "number",
"title": "Height",
"description": "Height of person in cm.",
"ancestors": [
"height"
],
"default": 175,
"maximum": 300,
"minimum": 0,
"multiple_of": null
},
{
"type": "boolean",
"title": "Alive",
"description": "Whether the person is (currently) alive.",
"ancestors": [
"alive"
],
"default": true
},
{
"type": "number",
"title": "Weight",
"description": "The person's weight in kg.",
"ancestors": [
"weight"
],
"default": null,
"maximum": null,
"minimum": 0,
"multiple_of": null
},
{
"type": "array",
"title": "Leg Lengths",
"description": "The length of the person's left and right legs in cm.",
"ancestors": [
"leg_lengths"
]
},
{
"type": "composite",
"title": "Hobby",
"description": "The person's only hobby.",
"ancestors": [
"hobby"
]
},
{
"type": "string",
"title": "Name",
"description": "Name of the activity.",
"ancestors": [
"hobby",
"name"
],
"default": ""
},
{
"type": "boolean",
"title": "Active",
"description": "Does the person actively engage with it?",
"ancestors": [
"hobby",
"active"
],
"default": null
},
{
"type": "integer",
"title": "Experience",
"description": "Experience practising it in years.",
"ancestors": [
"hobby",
"experience"
],
"default": null,
"maximum": 120,
"minimum": 0,
"multiple_of": null
}
]
Loading