fantaxy commited on
Commit
700f68a
Β·
verified Β·
1 Parent(s): fe68dba

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +15 -17
app.py CHANGED
@@ -1,8 +1,7 @@
1
  """
2
- app.py ―――――――――――――――――――――――――――――――――――――――――――――――――――――――
3
- ● πŸ”„ Preview JSON : μ½”λ“œ 창에 ν˜„μž¬ μ›Œν¬ν”Œλ‘œ JSON ν‘œμ‹œ
4
- ● πŸ’Ύ Save JSON : JSON 파일 λ‹€μš΄λ‘œλ“œ
5
- ● πŸ“‚ Load JSON : JSON μ—…λ‘œλ“œ ➜ λ…Έλ“œΒ·μ’Œν‘œΒ·μ—°κ²° μ¦‰μ‹œ 볡원
6
  """
7
 
8
  import os, json, typing, tempfile
@@ -10,14 +9,12 @@ import gradio as gr
10
  from gradio_workflowbuilder import WorkflowBuilder
11
 
12
  # -------------------------------------------------------------------
13
- # πŸ› οΈ 헬퍼 ν•¨μˆ˜
14
  # -------------------------------------------------------------------
15
  def export_pretty(data: typing.Dict[str, typing.Any]) -> str:
16
- """μ½”λ“œλ·° 미리보기용 λ¬Έμžμ—΄."""
17
  return json.dumps(data, indent=2, ensure_ascii=False) if data else "No workflow to export"
18
 
19
- def export_file(data: typing.Dict[str, typing.Any]) -> str | None:
20
- """μž„μ‹œ JSON 파일 경둜 λ°˜ν™˜(DownloadButton 용)."""
21
  if not data:
22
  return None
23
  fd, path = tempfile.mkstemp(suffix=".json")
@@ -25,17 +22,19 @@ def export_file(data: typing.Dict[str, typing.Any]) -> str | None:
25
  json.dump(data, f, ensure_ascii=False, indent=2)
26
  return path
27
 
28
- def import_workflow(file_obj: gr.File) -> tuple[gr.Update | None, str]:
29
- """μ—…λ‘œλ“œλœ JSON β†’ WorkflowBuilder κ°’ & μ½”λ“œλ·° λ™μ‹œ κ°±μ‹ ."""
 
 
 
30
  if file_obj is None:
31
  return None, "No workflow loaded"
32
  with open(file_obj.name, "r", encoding="utf-8") as f:
33
  data = json.load(f)
34
- # gr.update 둜 감싸야 μ‹œκ°μ μœΌλ‘œ μž¬λ Œλ”λ§λ¨
35
  return gr.update(value=data), json.dumps(data, indent=2, ensure_ascii=False)
36
 
37
  # -------------------------------------------------------------------
38
- # 🎨 UI (인라인 CSS)
39
  # -------------------------------------------------------------------
40
  CSS = """
41
  .main-container{max-width:1600px;margin:0 auto;}
@@ -62,7 +61,7 @@ CSS = """
62
  with gr.Blocks(title="🎨 Visual Workflow Builder", theme=gr.themes.Soft(), css=CSS) as demo:
63
 
64
  with gr.Column(elem_classes=["main-container"]):
65
- gr.Markdown("# 🎨 Visual Workflow Builder \n**Create sophisticated workflows with drag-and-drop simplicity.**")
66
 
67
  gr.HTML(
68
  """
@@ -82,7 +81,7 @@ with gr.Blocks(title="🎨 Visual Workflow Builder", theme=gr.themes.Soft(), css
82
  info="Drag from output ➜ input β€’ Click nodes to edit properties",
83
  )
84
 
85
- # ─── Export / Import 컨트둀 ───
86
  gr.Markdown("## πŸ’Ύ Export Β· πŸ“‚ Import")
87
 
88
  with gr.Row(elem_classes=["button-row"]):
@@ -97,12 +96,11 @@ with gr.Blocks(title="🎨 Visual Workflow Builder", theme=gr.themes.Soft(), css
97
  btn_download.click(fn=export_file, inputs=workflow_builder)
98
  file_upload.change(fn=import_workflow, inputs=file_upload, outputs=[workflow_builder, code_view])
99
 
100
- # μ‚¬μš©λ²•
101
  with gr.Accordion("πŸ“– How to Use", open=False):
102
  gr.Markdown(
103
  """
104
- 1. **Add Components** β€” drag items from the sidebar onto the canvas
105
- 2. **Connect Nodes** β€” drag from blue handle (output) ➜ gray handle (input)
106
  3. **Edit** β€” click any node to change its properties
107
  4. **Save** β€” *Save JSON file* to download your workflow
108
  5. **Load** β€” *Load JSON* to restore (nodes auto-position)
 
1
  """
2
+ app.py ――――――――――――――――――――――――――――――――――――――――――――――――
3
+ βœ“ Preview Β· Save Β· Load κΈ°λŠ₯ 포함
4
+ βœ“ Load μ‹œ λ…Έλ“œ/μ’Œν‘œ/μ—£μ§€ μžλ™ 볡원
 
5
  """
6
 
7
  import os, json, typing, tempfile
 
9
  from gradio_workflowbuilder import WorkflowBuilder
10
 
11
  # -------------------------------------------------------------------
12
+ # πŸ› οΈ 헬퍼
13
  # -------------------------------------------------------------------
14
  def export_pretty(data: typing.Dict[str, typing.Any]) -> str:
 
15
  return json.dumps(data, indent=2, ensure_ascii=False) if data else "No workflow to export"
16
 
17
+ def export_file(data: typing.Dict[str, typing.Any]) -> typing.Optional[str]:
 
18
  if not data:
19
  return None
20
  fd, path = tempfile.mkstemp(suffix=".json")
 
22
  json.dump(data, f, ensure_ascii=False, indent=2)
23
  return path
24
 
25
+ def import_workflow(file_obj: gr.File) -> typing.Tuple[typing.Any, str]:
26
+ """
27
+ μ—…λ‘œλ“œλœ JSON β†’ WorkflowBuilder κ°’ & μ½”λ“œλ·° κ°±μ‹ .
28
+ 첫 λ°˜ν™˜κ°’μ€ gr.update(value=...) 둜 감싸 μ‹œκ°μ  재배치 μœ λ„.
29
+ """
30
  if file_obj is None:
31
  return None, "No workflow loaded"
32
  with open(file_obj.name, "r", encoding="utf-8") as f:
33
  data = json.load(f)
 
34
  return gr.update(value=data), json.dumps(data, indent=2, ensure_ascii=False)
35
 
36
  # -------------------------------------------------------------------
37
+ # 🎨 CSS
38
  # -------------------------------------------------------------------
39
  CSS = """
40
  .main-container{max-width:1600px;margin:0 auto;}
 
61
  with gr.Blocks(title="🎨 Visual Workflow Builder", theme=gr.themes.Soft(), css=CSS) as demo:
62
 
63
  with gr.Column(elem_classes=["main-container"]):
64
+ gr.Markdown("# 🎨 Visual Workflow Builder\n**Create sophisticated workflows with drag-and-drop simplicity.**")
65
 
66
  gr.HTML(
67
  """
 
81
  info="Drag from output ➜ input β€’ Click nodes to edit properties",
82
  )
83
 
84
+ # ─── Export / Import ───
85
  gr.Markdown("## πŸ’Ύ Export Β· πŸ“‚ Import")
86
 
87
  with gr.Row(elem_classes=["button-row"]):
 
96
  btn_download.click(fn=export_file, inputs=workflow_builder)
97
  file_upload.change(fn=import_workflow, inputs=file_upload, outputs=[workflow_builder, code_view])
98
 
 
99
  with gr.Accordion("πŸ“– How to Use", open=False):
100
  gr.Markdown(
101
  """
102
+ 1. **Add Nodes** β€” drag items from the sidebar onto the canvas
103
+ 2. **Connect** β€” blue handle (output) ➜ gray handle (input)
104
  3. **Edit** β€” click any node to change its properties
105
  4. **Save** β€” *Save JSON file* to download your workflow
106
  5. **Load** β€” *Load JSON* to restore (nodes auto-position)