CoolFace
Apppublic

SebastianBravo/simci_css

sourceHugging Faceapache-2.0updated 2y agoView on Hugging Face
4likes
theme_dropdown.py58 linesDownload Raw Back to root
1import os2import pathlib3 4from gradio.themes.utils import ThemeAsset5 6 7def create_theme_dropdown():8    import gradio as gr9 10    asset_path = pathlib.Path(__file__).parent / "themes"11    themes = []12    for theme_asset in os.listdir(str(asset_path)):13        themes.append(14            (ThemeAsset(theme_asset), gr.Theme.load(str(asset_path / theme_asset)))15        )16 17    def make_else_if(theme_asset):18        return f"""19        else if (theme == '{str(theme_asset[0].version)}') {{20            var theme_css = `{theme_asset[1]._get_theme_css()}`21        }}"""22 23    head, tail = themes[0], themes[1:]24    if_statement = f"""25        if (theme == "{str(head[0].version)}") {{26            var theme_css = `{head[1]._get_theme_css()}`27        }} {" ".join(make_else_if(t) for t in tail)}28    """29 30    latest_to_oldest = sorted([t[0] for t in themes], key=lambda asset: asset.version)[31        ::-132    ]33    latest_to_oldest = [str(t.version) for t in latest_to_oldest]34 35    component = gr.Dropdown(36        choices=latest_to_oldest,37        value=latest_to_oldest[0],38        render=False,39        label="Select Version",40    ).style(container=False)41 42    return (43        component,44        f"""45        (theme) => {{46            if (!document.querySelector('.theme-css')) {{47                var theme_elem = document.createElement('style');48                theme_elem.classList.add('theme-css');49                document.head.appendChild(theme_elem);50            }} else {{51                var theme_elem = document.querySelector('.theme-css');52            }}53            {if_statement}54            theme_elem.innerHTML = theme_css;55        }}56    """,57    )58