<@ULVA73B9P> i would like to knw if it is posible ...
# ask-marvin
p
@Marvin i would like to knw if it is posible to generate an input variable in the paraemters dashaboard, with a button that make possible to select to use a dictionary as a variable or a float, like the boolean toggle, with useing in both of them values by default
m
thought for 271.3 seconds
Short answer: yes. In Prefect 3.x, if you type a flow parameter as a Union (e.g.,
Union[dict, float]
), the Parameters UI renders a toggle that lets you pick which type to use. The toggle auto-selects the branch that matches the parameter’s default value. Key behaviors: - Union[dict, float] → UI shows a toggle with “dict” and “float” - Only one default exists at the parameter level; the UI picks the branch that matches that default - If you want different defaults to appear when switching between options, use a Union of Pydantic models where each branch has its own field defaults Two patterns you can use: 1) Minimal: primitive Union (one overall default)
Copy code
from typing import Union
from prefect import flow

@flow
def my_flow(param: Union[dict, float] = 3.14):
    # If the user toggles to dict in the UI, they’ll enter a dict manually
    return param
- The UI will default to “float” (3.14). - If the user switches to “dict”, there isn’t a separate per-option default to auto-fill. 2) Recommended for per-option defaults: Union of Pydantic models
Copy code
from typing import Union
from pydantic import BaseModel, ConfigDict, Field
from prefect import flow

class DictOption(BaseModel):
    model_config = ConfigDict(json_schema_extra={"title": "Dictionary"})
    data: dict = Field(default={"a": 1, "b": 2})

class FloatOption(BaseModel):
    model_config = ConfigDict(json_schema_extra={"title": "Float"})
    value: float = Field(default=0.5)

@flow
def my_flow(param: Union[DictOption, FloatOption] = FloatOption()):
    # UI shows a toggle: "Dictionary" | "Float"
    # - Start on "Float" with value 0.5
    # - If the user switches to "Dictionary", the form shows {"a": 1, "b": 2}
    return param
- Each branch has its own defaults, so switching the toggle shows sensible defaults per option. - You can customize the toggle labels via the model title (shown above). Docs: - Form building with Pydantic models - Flow parameters and type validation - Interactive forms (same form engine) Questions to confirm so I can tailor this: - Are you on Prefect 3.x? - Do you need separate defaults to appear when the user switches between “dict” and “float”? If so, I recommend the Pydantic-models approach above and can adapt it to your exact shapes.
p
If this was my original input
Copy code
mip_ratio_thresholds: Optional[Dict[str, confloat(ge=0, le=1)]] = Field(
    default={
        "0": 0.10, "1": 0.35, "2": 0.50, "3": 0.20, "4": 0.80, "5": 0.45, "6": 0.60, "7": 0.30, "8": 0.15,
        "9": 0.55, "10": 0.25, "11": 0.70, "12": 0.40, "13": 0.90, "14": 0.65, "15": 0.75, "16": 0.85,
        "17": 0.33, "18": 0.12, "19": 0.58, "20": 0.22, "21": 0.48, "22": 0.95, "23": 0.52, "24": 0.18,
        "25": 0.28, "26": 0.68, "27": 0.88},
    description=(
        "Diccionario opcional donde la clave es el nombre de la instalación y el valor es un número en [0, 1] "
        "que indica la fracción del MIP a aplicar para esa instalación concreta. "
        "Solo se utilizará cuando 'Usar filtro con mip' esté activado."
    ),
    title="Umbrales específicos de MIP por instalación (fracción en [0, 1])"
)
how could i ocnvert it to the structure i just mentioned you?
If this was my original input
Copy code
mip_ratio_thresholds: Optional[Dict[str, confloat(ge=0, le=1)]] = Field(
    default={
        "0": 0.10, "1": 0.35, "2": 0.50, "3": 0.20, "4": 0.80, "5": 0.45, "6": 0.60, "7": 0.30, "8": 0.15,
        "9": 0.55, "10": 0.25, "11": 0.70, "12": 0.40, "13": 0.90, "14": 0.65, "15": 0.75, "16": 0.85,
        "17": 0.33, "18": 0.12, "19": 0.58, "20": 0.22, "21": 0.48, "22": 0.95, "23": 0.52, "24": 0.18,
        "25": 0.28, "26": 0.68, "27": 0.88},
    description=(
        "Diccionario opcional donde la clave es el nombre de la instalación y el valor es un número en [0, 1] "
        "que indica la fracción del MIP a aplicar para esa instalación concreta. "
        "Solo se utilizará cuando 'Usar filtro con mip' esté activado."
    ),
    title="Umbrales específicos de MIP por instalación (fracción en [0, 1])"
)
how could i transform it into the two paraemter input with this dictionary and float value?