Skip to content

Commit f2392f5

Browse files
committed
fix(playground): fix selector blank display for router update and creation (load balancing strategy)
1 parent 69d8a62 commit f2392f5

File tree

3 files changed

+34
-28
lines changed

3 files changed

+34
-28
lines changed

playground/app/features/routers/components/forms.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -33,11 +33,11 @@ def router_settings_form_fields() -> rx.Component:
3333
),
3434
entity_form_select_field(
3535
label="Load balancing strategy",
36-
items=RoutersState.router_load_balancing_strategies_list,
36+
value_labels={"shuffle": "Shuffle", "least_busy": "Least busy"},
3737
value=RoutersState.entity.load_balancing_strategy,
3838
on_change=lambda value: RoutersState.set_edit_entity_attribut("load_balancing_strategy", value),
3939
tooltip="Strategy to use for load balancing between providers of the router",
40-
disable=RoutersState.edit_entity_loading,
40+
disabled=RoutersState.edit_entity_loading,
4141
),
4242
entity_form_input_field(
4343
label="Prompt tokens cost",
@@ -106,7 +106,7 @@ def router_create_form_fields() -> rx.Component:
106106
),
107107
entity_form_select_field(
108108
label="Load balancing strategy",
109-
items=RoutersState.router_load_balancing_strategies_list,
109+
value_labels={"shuffle": "Shuffle", "least_busy": "Least busy"},
110110
value=RoutersState.entity_to_create.load_balancing_strategy,
111111
on_change=lambda value: RoutersState.set_new_entity_attribut("load_balancing_strategy", value),
112112
tooltip="Strategy to use for load balancing between providers of the router",

playground/app/features/routers/state.py

Lines changed: 4 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -26,11 +26,6 @@ def router_types_list(self) -> list[str]:
2626
]
2727
)
2828

29-
@rx.var
30-
def router_load_balancing_strategies_list(self) -> list[str]:
31-
"""Get list of router load balancing strategies."""
32-
return ["Shuffle", "Least busy"]
33-
3429
############################################################
3530
# Load entities
3631
############################################################
@@ -40,17 +35,13 @@ def router_load_balancing_strategies_list(self) -> list[str]:
4035
def _format_router(self, router: dict) -> Router:
4136
"""Format router."""
4237

43-
_load_balancing_strategy_converter = {
44-
"shuffle": "Shuffle",
45-
"least_busy": "Least busy",
46-
}
4738
return Router(
4839
id=router["id"],
4940
name=router["name"],
5041
user=self.router_owners[router["user_id"]],
5142
type=router["type"],
5243
aliases=",".join(router["aliases"]) if router["aliases"] else "",
53-
load_balancing_strategy=_load_balancing_strategy_converter.get(router["load_balancing_strategy"]),
44+
load_balancing_strategy=router["load_balancing_strategy"],
5445
max_context_length=router["max_context_length"],
5546
vector_size=router["vector_size"],
5647
cost_prompt_tokens=router["cost_prompt_tokens"],
@@ -182,7 +173,7 @@ async def delete_entity(self):
182173
############################################################
183174
entity_to_create: Router = Router(
184175
type="text-generation",
185-
load_balancing_strategy="Shuffle",
176+
load_balancing_strategy="shuffle",
186177
cost_prompt_tokens=0.0,
187178
cost_completion_tokens=0.0,
188179
)
@@ -205,12 +196,10 @@ async def create_entity(self):
205196
self.create_entity_loading = True
206197
yield
207198

208-
new_router_load_balancing_strategy = self.entity_to_create.load_balancing_strategy.lower().replace(" ", "_")
209-
210199
payload = {
211200
"name": self.entity_to_create.name,
212201
"type": self.entity_to_create.type,
213-
"load_balancing_strategy": new_router_load_balancing_strategy,
202+
"load_balancing_strategy": self.entity_to_create.load_balancing_strategy,
214203
"cost_prompt_tokens": self.entity_to_create.cost_prompt_tokens,
215204
"cost_completion_tokens": self.entity_to_create.cost_completion_tokens,
216205
}
@@ -276,13 +265,12 @@ async def edit_entity(self):
276265
yield
277266

278267
router_aliases = [alias.strip() for alias in self.entity.aliases.split(",") if alias.strip()]
279-
router_load_balancing_strategy = self.entity.load_balancing_strategy.lower().replace(" ", "_")
280268

281269
payload = {
282270
"name": self.entity.name,
283271
"type": self.entity.type,
284272
"aliases": router_aliases,
285-
"load_balancing_strategy": router_load_balancing_strategy,
273+
"load_balancing_strategy": self.entity.load_balancing_strategy,
286274
"cost_prompt_tokens": self.entity.cost_prompt_tokens,
287275
"cost_completion_tokens": self.entity.cost_completion_tokens,
288276
}

playground/app/shared/components/forms.py

Lines changed: 27 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -8,11 +8,12 @@
88

99
def entity_form_select_field(
1010
label: str,
11-
items: rx.var,
11+
items: rx.var = None,
1212
value: Any = None,
1313
on_change: Callable | None = None,
1414
disabled: bool = False,
1515
tooltip: str | None = None,
16+
value_labels: dict[str, str] | None = None,
1617
**kwargs,
1718
) -> rx.Component:
1819
normalized_value: Any = ""
@@ -23,6 +24,30 @@ def entity_form_select_field(
2324
else:
2425
normalized_value = value
2526

27+
placeholder = kwargs.pop("placeholder", None)
28+
29+
if value_labels is not None:
30+
trigger = rx.select.trigger(placeholder=placeholder, width="100%") if placeholder else rx.select.trigger(width="100%")
31+
select_component = rx.select.root(
32+
trigger,
33+
rx.select.content(
34+
*[rx.select.item(display, value=val) for val, display in value_labels.items()],
35+
),
36+
value=normalized_value,
37+
on_change=on_change,
38+
disabled=disabled,
39+
)
40+
else:
41+
select_component = rx.select(
42+
items=items,
43+
value=normalized_value,
44+
on_change=on_change,
45+
disabled=disabled,
46+
placeholder=placeholder,
47+
**kwargs,
48+
width="100%",
49+
)
50+
2651
return rx.vstack(
2752
rx.cond(
2853
bool(tooltip),
@@ -37,14 +62,7 @@ def entity_form_select_field(
3762
),
3863
rx.text(label, size=TEXT_SIZE_LABEL, weight="bold"),
3964
),
40-
rx.select(
41-
items=items,
42-
value=normalized_value,
43-
on_change=on_change,
44-
disabled=disabled,
45-
**kwargs,
46-
width="100%",
47-
),
65+
select_component,
4866
spacing=SPACING_TINY,
4967
width="100%",
5068
)

0 commit comments

Comments
 (0)