File size: 999 Bytes
193db9d
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
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)


# List utilities
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]