File size: 1,247 Bytes
a8c39f5
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
import json
import os
import importlib
import gradio as gr
import sys

now_dir = os.getcwd()
folder = os.path.join(
    os.path.dirname(os.path.dirname(os.path.dirname(os.path.abspath(__file__)))),
    "assets",
    "themes",
)
config_file = os.path.join(now_dir, "assets", "config.json")

sys.path.append(folder)


def read_json_file(filename):
    """Helper function to read a JSON file and return its contents."""
    with open(filename, "r", encoding="utf8") as json_file:
        return json.load(json_file)


def load_theme():
    """Load the selected theme based on the configuration file."""
    try:
        config_data = read_json_file(config_file)
        selected_file = config_data["theme"]["file"]
        class_name = config_data["theme"]["class"]

        if class_name:
            if selected_file:
                module = importlib.import_module(selected_file[:-3])
                obtained_class = getattr(module, class_name)
                return obtained_class()
            else:
                return class_name
        else:
            print("No valid theme class found.")
            return None

    except Exception as error:
        print(f"An error occurred while loading the theme: {error}")
        return None