Skip to content

Commit be97de1

Browse files
author
legionfu
committed
save flow
1 parent eb90c1e commit be97de1

File tree

2 files changed

+53
-11
lines changed

2 files changed

+53
-11
lines changed

javascript/state.core.js

Lines changed: 22 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -761,16 +761,34 @@ state.core = (function () {
761761
filename += ".flow";
762762
}
763763
if(filename != ".flow"){
764-
let stored_config = get_js_local_data()
764+
765765
let data = {
766766
method: 'POST',
767767
headers: { 'Content-Type': 'application/json' },
768768
body: JSON.stringify({
769-
"file_name":filename,
770-
"file_data":stored_config
769+
"file_path":filename
771770
})
772771
}
773-
fetch("/lightdiffusionflow/local/save_flow_to_local",data)
772+
fetch(`/lightdiffusionflow/local/file_exist`, data)
773+
.then(response => response.json())
774+
.then(data => {
775+
console.log("file_exist")
776+
console.log(data)
777+
if(!data || (data && confirm("Overwrite the existing file with the same name?")))
778+
{
779+
let stored_config = get_js_local_data()
780+
data = {
781+
method: 'POST',
782+
headers: {'Content-Type': 'application/json'},
783+
body: JSON.stringify({
784+
"file_name":filename,
785+
"file_data":stored_config,
786+
"overwrite":true
787+
})
788+
}
789+
fetch("/lightdiffusionflow/local/save_flow_to_local",data)
790+
}
791+
});
774792
}
775793
},
776794

scripts/state_api.py

Lines changed: 31 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,7 @@
5959
paste_symbol = '\u2199\ufe0f' # ↙
6060
refresh_symbol = '\U0001f504' # 🔄
6161
save_style_symbol = '\U0001f4be' # 💾
62+
clear_prompt_symbol = '\U0001f5d1\ufe0f' # 🗑️
6263
apply_style_symbol = '\U0001f4cb' # 📋
6364

6465

@@ -100,13 +101,14 @@ def test_func():
100101

101102
# print(parameters_copypaste.paste_fields)
102103

103-
# 直接从http请求带参数过来触发不了弹窗?
104-
# 为了正常显示弹窗只能改成全局参数。
104+
105+
# fastapi触发不了弹窗
105106
def custom_msg_box():
106107
global g_msg_info
107108
if(g_msg_info != ""):
108109
print(f"gr.Info({g_msg_info})")
109110
gr.Info(g_msg_info)
111+
g_msg_info = ""
110112

111113
def clear_markup(html_str):
112114
clearly_str = html_str
@@ -122,7 +124,7 @@ def add_output_log(msg:str="", style:str=""):
122124
print(clear_msg)
123125
Output_Log += f"<p style='color:rgb(192,192,192);{style}'>{msg}</p>"
124126

125-
custom_msg_box()
127+
#custom_msg_box()
126128
return Output_Log, Output_Log
127129

128130
def add_output_warning(msg:str=""):
@@ -245,6 +247,14 @@ def apply_local_flow(selected):
245247
Need_Preload = True
246248
gr.Info(clear_markup(OutputPrompt.startimport()))
247249

250+
def delete_local_flow(selected):
251+
global local_flow_list,local_flows_path
252+
if(selected != "" and selected != None):
253+
flow_path = os.path.join(data_path, local_flows_path, selected)
254+
if(os.path.exists(flow_path)):
255+
os.remove(flow_path)
256+
print("Local File Deleted!")
257+
248258
def set_lightdiffusionflow_file():
249259
global Preload_File
250260
return Preload_File
@@ -830,6 +840,7 @@ class file_params(BaseModel):
830840
class savefile_params(BaseModel):
831841
file_name:str
832842
file_data:dict
843+
overwrite:bool
833844

834845
class StateApi():
835846

@@ -864,6 +875,7 @@ def start(self, _: gr.Blocks, app: FastAPI):
864875
self.add_api_route('/local/png_info', self.png_info, methods=['POST']) #
865876
# 传入一个文件路径,返回文件内容
866877
self.add_api_route('/local/read_file', self.read_file, methods=['POST'])
878+
self.add_api_route('/local/file_exist', self.file_exist, methods=['POST'])
867879
self.add_api_route('/local/need_preload', self.need_preload, methods=['GET'])
868880
# 保存当前配置到本地文件夹
869881
self.add_api_route('/local/save_flow_to_local', self.saveFlowToLocal, methods=['POST'])
@@ -994,8 +1006,16 @@ def read_file(self, params:file_params):
9941006
file_content = ""
9951007
with open(params.file_path, mode='r', encoding='UTF-8') as f:
9961008
file_content = f.read()
997-
998-
return file_content
1009+
1010+
def file_exist(self, params:file_params):
1011+
print("file_exist")
1012+
if(not os.path.exists(params.file_path)):
1013+
flow_path = os.path.join(data_path, local_flows_path, params.file_path)
1014+
if(os.path.exists(flow_path)):
1015+
return True
1016+
else:
1017+
return False
1018+
return False
9991019

10001020
def get_img_elem_key(self):
10011021
global extensions_id_conponents
@@ -1085,7 +1105,7 @@ def saveFlowToLocal(self, data_to_save:savefile_params):
10851105

10861106
flow_path = os.path.join(data_path, local_flows_path, data_to_save.file_name)
10871107
print(flow_path)
1088-
if(not os.path.exists(flow_path)):
1108+
if(not os.path.exists(flow_path) or (data_to_save.overwrite)):
10891109
with open(flow_path,"w") as f:
10901110
#json.dump(overall_data,f)
10911111
f.write(json.dumps(overall_data, ensure_ascii=False, indent=4))
@@ -1236,6 +1256,7 @@ def after_component(self, component, **kwargs):
12361256
State_Comps["refresh"][i].click(refresh_local_flows, inputs=State_Comps["local_flows"],outputs=State_Comps["local_flows"])
12371257
State_Comps["apply"][i].click(apply_local_flow, inputs=[State_Comps["local_flows"][i]],outputs=[])
12381258
State_Comps["save"][i].click(fn=None,_js="state.core.actions.saveFlowToLocal", inputs=[],outputs=[])
1259+
#State_Comps["delete"][i].click(delete_local_flow, inputs=[State_Comps["local_flows"][i]],outputs=State_Comps["local_flows"])
12391260

12401261

12411262
for btn in State_Comps["export"]:
@@ -1309,6 +1330,7 @@ def custom_ui(self):
13091330
State_Comps["outlog"]
13101331
State_Comps["local_flows"]
13111332
State_Comps["apply"]
1333+
State_Comps["delete"]
13121334
State_Comps["save"]
13131335
State_Comps["refresh"]
13141336
except:
@@ -1317,6 +1339,7 @@ def custom_ui(self):
13171339
State_Comps["outlog"] = []
13181340
State_Comps["local_flows"] = []
13191341
State_Comps["apply"] = []
1342+
State_Comps["delete"] = []
13201343
State_Comps["save"] = []
13211344
State_Comps["refresh"] = []
13221345

@@ -1334,8 +1357,9 @@ def custom_ui(self):
13341357
with gr.Accordion('LightDiffusionFlow '+lightdiffusionflow_version.lightdiffusionflow_version + save_mode, open=True, visible=True, elem_id=cur_mode+'_lightdiffusionflow'):
13351358

13361359
with gr.Row():
1337-
State_Comps["local_flows"].append(gr.Dropdown(label="", show_label=False ,choices=local_flow_list,value='',elem_id=cur_mode+'_ldf_local_flows'))
1360+
State_Comps["local_flows"].append(gr.Dropdown(label="", show_label=False , multiselect=False, choices=local_flow_list,value='',elem_id=cur_mode+'_ldf_local_flows'))
13381361
State_Comps["apply"].append(ui_components.ToolButton(value=paste_symbol,elem_id=cur_mode+'_ldf_apply'))
1362+
#State_Comps["delete"].append(ui_components.ToolButton(value=clear_prompt_symbol,elem_id=cur_mode+'_ldf_delete'))
13391363
State_Comps["save"].append(ui_components.ToolButton(value=save_style_symbol,elem_id=cur_mode+'_ldf_save'))
13401364
State_Comps["refresh"].append(ui_components.ToolButton(value=refresh_symbol,elem_id=cur_mode+'_ldf_refresh'))
13411365

0 commit comments

Comments
 (0)