|
from typing import Any, Literal |
|
|
|
import gradio as gr |
|
|
|
DIRECTIONS = Literal["up", "down"] |
|
|
|
|
|
def make_state(value: Any) -> gr.State: |
|
"""Make a state from a value.""" |
|
if isinstance(value, gr.State): |
|
return value |
|
else: |
|
return gr.State(value) |
|
|
|
|
|
|
|
def move_item(items: list, position: int, direction: DIRECTIONS): |
|
"""Move an item up or down in a list.""" |
|
if not isinstance(items, list): |
|
raise ValueError("items must be a list") |
|
if not isinstance(position, int) or not (0 <= position < len(items)): |
|
raise ValueError("position must be a valid index in the list") |
|
if direction not in ["up", "down"]: |
|
raise ValueError("direction must be 'up' or 'down'") |
|
|
|
if direction == "up" and position > 0: |
|
items[position], items[position - 1] = items[position - 1], items[position] |
|
elif direction == "down" and position < len(items) - 1: |
|
items[position], items[position + 1] = items[position + 1], items[position] |
|
|