[GH-ISSUE #20] Select style list to use (json files) #14

Open
opened 2026-10-30 21:03:11 +00:00 by bsakaguchi · 2 comments
Owner

Originally created by @JeromeGH on GitHub (Sep 26, 2023).
Original GitHub issue: https://github.com/ahgsql/StyleSelectorXL/issues/20

Please implement the option to select between several style lists (json files)

https://youtu.be/6aZONG75dr0?si=VQulP95bdRlYjH77&t=180

Originally created by @JeromeGH on GitHub (Sep 26, 2023). Original GitHub issue: https://github.com/ahgsql/StyleSelectorXL/issues/20 Please implement the option to select between several style lists (json files) https://youtu.be/6aZONG75dr0?si=VQulP95bdRlYjH77&t=180
Author
Owner

@benjamin-bertram commented on GitHub (Oct 3, 2023):

I really dig this features as well, so I tried to bring this to work, but my styles won't just get not updated. Any thoughts on this? This was my approach after 8hrs of work (I am not a coder though)

I added a simple

def getFile(): styleFile = [f for f in os.listdir(folder) if f.endswith('.json')] return styleFile

and updated the class and the ui so far, but I am stuck on the automated update. I can select my files from the dropdown, but nothing happens.

`class StyleSelectorXL(scripts.Script):
def init(self) -> None:
super().init()
self.styleFile = getFile()
self.initial_file = gr.State(self.styleFile[0] if self.styleFile else "sdxl_styles.json")
self.styleNames = self.getStyles(self.initial_file.value)

def getStyles(self, selected_json_file):
    global stylespath
    json_path = os.path.join(folder, selected_json_file)
    stylespath = json_path
    json_data = get_json_content(json_path)
    styles = read_sdxl_styles(json_data)
    return styles

def updateStyles(self, selected_json_file):
    self.initial_file.value = selected_json_file
    self.styleNames = self.getStyles(selected_json_file)
    print(f"Updated initial_file.value: {self.initial_file.value}")
    print(f"Updated styleNames: {self.styleNames}")

def title(self):
    return "Style Selector for SDXL 1.0"

def show(self, is_img2img):
    return scripts.AlwaysVisible

def ui(self, is_img2img):
    enabled = getattr(shared.opts, "enable_styleselector_by_default", True)

    with gr.Group():
        with gr.Accordion("SDXL Styles NEW", open=enabled):
            with FormRow():
                with FormColumn(min_width=160):
                    is_enabled = gr.Checkbox(
                        value=enabled, label="Enable Style Selector", info="Enable Or Disable Style Selector ")
                with FormColumn(elem_id="Randomize Style"):
                    randomize = gr.Checkbox(
                        value=False, label="Randomize Style", info="This Will Override Selected Style")
                with FormColumn(elem_id="Randomize For Each Iteration"):
                    randomizeEach = gr.Checkbox(
                        value=False, label="Randomize For Each Iteration", info="Every prompt in Batch Will Have Random Style")
                with FormColumn():
                    json_file = gr.Dropdown(
                        self.styleFile,
                        value=self.styleFile[0] if self.styleFile else "sdxl_styles.json",
                        live=True,
                        multiselect=False,
                        label="Select Stylefile",
                        change=lambda selected: [print(f"Selected JSON File: {selected}"), setattr(initial_file, 'json_file', selected), self.updateStyles(selected)]
                    )

            with FormRow():
                with FormColumn(min_width=160):
                    allstyles = gr.Checkbox(
                        value=False, label="Generate All Styles In Order", info="To Generate Your Prompt in All Available Styles, Its Better to set batch count to " + str(len(self.styleNames)) + " ( Style Count)")

            style_ui_type = shared.opts.data.get(
                "styles_ui",  "radio-buttons")

            if style_ui_type == "select-list":
                style = gr.Dropdown(
                    self.styleNames, value=self.styleNames[10], multiselect=False, label="Select Style")
            else:
                style = gr.Radio(
                    label='Style', choices=self.styleNames, value=self.styleNames[10])
                        
    # Ignore the error if the attribute is not present

    return [is_enabled, randomize, randomizeEach, allstyles, style]`
<!-- gh-comment-id:1745790682 --> @benjamin-bertram commented on GitHub (Oct 3, 2023): I really dig this features as well, so I tried to bring this to work, but my styles won't just get not updated. Any thoughts on this? This was my approach after 8hrs of work (I am not a coder though) I added a simple `def getFile(): styleFile = [f for f in os.listdir(folder) if f.endswith('.json')] return styleFile` and updated the class and the ui so far, but I am stuck on the automated update. I can select my files from the dropdown, but nothing happens. `class StyleSelectorXL(scripts.Script): def __init__(self) -> None: super().__init__() self.styleFile = getFile() self.initial_file = gr.State(self.styleFile[0] if self.styleFile else "sdxl_styles.json") self.styleNames = self.getStyles(self.initial_file.value) def getStyles(self, selected_json_file): global stylespath json_path = os.path.join(folder, selected_json_file) stylespath = json_path json_data = get_json_content(json_path) styles = read_sdxl_styles(json_data) return styles def updateStyles(self, selected_json_file): self.initial_file.value = selected_json_file self.styleNames = self.getStyles(selected_json_file) print(f"Updated initial_file.value: {self.initial_file.value}") print(f"Updated styleNames: {self.styleNames}") def title(self): return "Style Selector for SDXL 1.0" def show(self, is_img2img): return scripts.AlwaysVisible def ui(self, is_img2img): enabled = getattr(shared.opts, "enable_styleselector_by_default", True) with gr.Group(): with gr.Accordion("SDXL Styles NEW", open=enabled): with FormRow(): with FormColumn(min_width=160): is_enabled = gr.Checkbox( value=enabled, label="Enable Style Selector", info="Enable Or Disable Style Selector ") with FormColumn(elem_id="Randomize Style"): randomize = gr.Checkbox( value=False, label="Randomize Style", info="This Will Override Selected Style") with FormColumn(elem_id="Randomize For Each Iteration"): randomizeEach = gr.Checkbox( value=False, label="Randomize For Each Iteration", info="Every prompt in Batch Will Have Random Style") with FormColumn(): json_file = gr.Dropdown( self.styleFile, value=self.styleFile[0] if self.styleFile else "sdxl_styles.json", live=True, multiselect=False, label="Select Stylefile", change=lambda selected: [print(f"Selected JSON File: {selected}"), setattr(initial_file, 'json_file', selected), self.updateStyles(selected)] ) with FormRow(): with FormColumn(min_width=160): allstyles = gr.Checkbox( value=False, label="Generate All Styles In Order", info="To Generate Your Prompt in All Available Styles, Its Better to set batch count to " + str(len(self.styleNames)) + " ( Style Count)") style_ui_type = shared.opts.data.get( "styles_ui", "radio-buttons") if style_ui_type == "select-list": style = gr.Dropdown( self.styleNames, value=self.styleNames[10], multiselect=False, label="Select Style") else: style = gr.Radio( label='Style', choices=self.styleNames, value=self.styleNames[10]) # Ignore the error if the attribute is not present return [is_enabled, randomize, randomizeEach, allstyles, style]`
Author
Owner

@benjamin-bertram commented on GitHub (Oct 5, 2023):

I got something working here:
https://github.com/benjamin-bertram/StyleSelectorXL_JsonSelector

<!-- gh-comment-id:1748758441 --> @benjamin-bertram commented on GitHub (Oct 5, 2023): I got something working here: https://github.com/benjamin-bertram/StyleSelectorXL_JsonSelector
Sign in to join this conversation.
No milestone
No project
No assignees
1 participant
Notifications
Due date
The due date is invalid or out of range. Please use the format "yyyy-mm-dd".

No due date set.

Dependencies

No dependencies set.

Reference
ahgsql/StyleSelectorXL#14
No description provided.