fantaxy commited on
Commit
310bef9
Β·
verified Β·
1 Parent(s): 1635f86

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +92 -99
app.py CHANGED
@@ -1,126 +1,119 @@
 
1
  import gradio as gr
2
  from gradio_workflowbuilder import WorkflowBuilder
3
- import json
4
 
 
 
 
 
 
 
5
 
6
- def export_workflow(workflow_data):
7
- """Export workflow as formatted JSON"""
8
- if not workflow_data:
9
- return "No workflow to export"
10
- return json.dumps(workflow_data, indent=2)
 
 
 
11
 
 
 
 
 
 
 
 
12
 
13
- # Create the main interface
 
 
14
  with gr.Blocks(
15
- title="🎨 Visual Workflow Builder",
16
  theme=gr.themes.Soft(),
17
  css="""
18
  .main-container { max-width: 1600px; margin: 0 auto; }
19
  .workflow-section { margin-bottom: 2rem; }
20
  .button-row { display: flex; gap: 1rem; justify-content: center; margin: 1rem 0; }
21
-
22
  .component-description {
23
  padding: 24px;
24
- background: linear-gradient(135deg, #f8fafc 0%, #e2e8f0 100%);
25
- border-radius: 12px;
26
- border-left: 4px solid #3b82f6;
27
- margin: 16px 0;
28
- font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', sans-serif;
29
- box-shadow: 0 2px 8px rgba(0, 0, 0, 0.05);
30
- }
31
-
32
- .component-description p {
33
- margin: 10px 0;
34
- line-height: 1.6;
35
- color: #374151;
36
- }
37
-
38
- .base-description {
39
- font-size: 17px;
40
- font-weight: 600;
41
- color: #1e293b;
42
- }
43
-
44
- .base-description strong {
45
- color: #3b82f6;
46
- font-weight: 700;
47
- }
48
-
49
- .feature-description {
50
- font-size: 16px;
51
- color: #1e293b;
52
- font-weight: 500;
53
- }
54
-
55
- .customization-note {
56
- font-size: 15px;
57
- color: #64748b;
58
- font-style: italic;
59
- }
60
-
61
- .sample-intro {
62
- font-size: 15px;
63
- color: #1e293b;
64
- font-weight: 600;
65
- margin-top: 16px;
66
- border-top: 1px solid #e2e8f0;
67
- padding-top: 16px;
68
  }
 
 
 
 
 
 
 
69
  """
70
  ) as demo:
71
-
72
  with gr.Column(elem_classes=["main-container"]):
73
- gr.Markdown("""
74
- # 🎨 Visual Workflow Builder
75
-
76
- **Create sophisticated workflows with drag and drop simplicity.**
77
- """)
78
-
79
- # Simple description section with styling
80
- gr.HTML("""
81
- <div class="component-description">
82
- <p class="base-description">Base custom component powered by <strong>svelteflow</strong>.</p>
83
- <p class="feature-description">Create custom workflows.</p>
84
- <p class="customization-note">You can customise the nodes as well as the design of the workflow.</p>
85
- <p class="sample-intro">Here is a sample:</p>
86
- </div>
87
- """)
88
-
89
- # Main workflow builder section
90
  with gr.Column(elem_classes=["workflow-section"]):
91
  workflow_builder = WorkflowBuilder(
92
  label="🎨 Visual Workflow Designer",
93
- info="Drag components from the sidebar β†’ Connect nodes by dragging from output (right) to input (left) β†’ Click nodes to edit properties"
94
  )
95
-
96
- # Export section below the workflow
97
- gr.Markdown("## πŸ’Ύ Export Workflow")
98
-
99
- with gr.Row():
100
- with gr.Column():
101
- export_output = gr.Code(
102
- language="json",
103
- label="Workflow Configuration",
104
- lines=10
105
- )
106
-
107
- # Action button
108
  with gr.Row(elem_classes=["button-row"]):
109
- export_btn = gr.Button("πŸ’Ύ Export JSON", variant="primary", size="lg")
110
-
111
- # Instructions
112
 
113
-
114
- # Event handlers
115
- export_btn.click(
116
- fn=export_workflow,
117
- inputs=[workflow_builder],
118
- outputs=[export_output]
119
- )
120
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
121
 
 
 
 
122
  if __name__ == "__main__":
123
- demo.launch(
124
- server_name="0.0.0.0",
125
- show_error=True
126
- )
 
1
+ import os, json, typing, tempfile
2
  import gradio as gr
3
  from gradio_workflowbuilder import WorkflowBuilder
 
4
 
5
+ # -------------------------------------------------------------------
6
+ # πŸ› οΈ 헬퍼
7
+ # -------------------------------------------------------------------
8
+ def export_pretty(data: typing.Dict[str, typing.Any]) -> str:
9
+ """μ½”λ“œλ·° 미리보기용 JSON λ¬Έμžμ—΄."""
10
+ return json.dumps(data, indent=2, ensure_ascii=False) if data else "No workflow to export"
11
 
12
+ def export_file(data: typing.Dict[str, typing.Any]) -> str | None:
13
+ """JSON을 μž„μ‹œ 파일둜 μ €μž₯ ν›„ 경둜 λ°˜ν™˜(DownloadButton에 전달)."""
14
+ if not data:
15
+ return None
16
+ fd, path = tempfile.mkstemp(suffix=".json")
17
+ with os.fdopen(fd, "w", encoding="utf-8") as f:
18
+ json.dump(data, f, ensure_ascii=False, indent=2)
19
+ return path
20
 
21
+ def import_workflow(file_obj: gr.File) -> tuple[dict | None, str]:
22
+ """μ—…λ‘œλ“œλœ JSON β†’ builder κ°’ & μ½”λ“œλ·° λ¬Έμžμ—΄ λ™μ‹œ λ°˜ν™˜."""
23
+ if file_obj is None:
24
+ return None, "No workflow loaded"
25
+ with open(file_obj.name, "r", encoding="utf-8") as f:
26
+ data = json.load(f)
27
+ return data, json.dumps(data, indent=2, ensure_ascii=False)
28
 
29
+ # -------------------------------------------------------------------
30
+ # 🎨 UI
31
+ # -------------------------------------------------------------------
32
  with gr.Blocks(
33
+ title="🎨 Visual Workflow Builder",
34
  theme=gr.themes.Soft(),
35
  css="""
36
  .main-container { max-width: 1600px; margin: 0 auto; }
37
  .workflow-section { margin-bottom: 2rem; }
38
  .button-row { display: flex; gap: 1rem; justify-content: center; margin: 1rem 0; }
 
39
  .component-description {
40
  padding: 24px;
41
+ background: linear-gradient(135deg,#f8fafc 0%,#e2e8f0 100%);
42
+ border-radius: 12px; border-left: 4px solid #3b82f6;
43
+ box-shadow: 0 2px 8px rgba(0,0,0,.05); margin: 16px 0;
44
+ font-family: -apple-system,BlinkMacSystemFont,'Segoe UI',sans-serif;
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
45
  }
46
+ .component-description p{margin:10px 0;line-height:1.6;color:#374151;}
47
+ .base-description{font-size:17px;font-weight:600;color:#1e293b;}
48
+ .base-description strong{color:#3b82f6;font-weight:700;}
49
+ .feature-description{font-size:16px;font-weight:500;color:#1e293b;}
50
+ .customization-note{font-size:15px;font-style:italic;color:#64748b;}
51
+ .sample-intro{font-size:15px;font-weight:600;color:#1e293b;margin-top:16px;
52
+ border-top:1px solid #e2e8f0;padding-top:16px;}
53
  """
54
  ) as demo:
55
+
56
  with gr.Column(elem_classes=["main-container"]):
57
+ gr.Markdown("# 🎨 Visual Workflow Builder\n**Create sophisticated workflows with drag-and-drop simplicity.**")
58
+
59
+ gr.HTML(
60
+ """
61
+ <div class="component-description">
62
+ <p class="base-description">Base custom component powered by <strong>svelteflow</strong>.</p>
63
+ <p class="feature-description">Create custom workflows.</p>
64
+ <p class="customization-note">You can customise the nodes as well as the design of the workflow.</p>
65
+ <p class="sample-intro">Here is a sample:</p>
66
+ </div>
67
+ """
68
+ )
69
+
70
+ # ─── Workflow Builder ───
 
 
 
71
  with gr.Column(elem_classes=["workflow-section"]):
72
  workflow_builder = WorkflowBuilder(
73
  label="🎨 Visual Workflow Designer",
74
+ info="Drag components from the sidebar β†’ connect β†’ click nodes to edit properties",
75
  )
76
+
77
+ # ─── Export / Import μ˜μ—­ ───
78
+ gr.Markdown("## πŸ’Ύ Export Β· πŸ“‚ Import")
79
+
 
 
 
 
 
 
 
 
 
80
  with gr.Row(elem_classes=["button-row"]):
81
+ btn_preview = gr.Button("πŸ”„ Preview JSON")
82
+ btn_download = gr.DownloadButton("πŸ’Ύ Save JSON file")
83
+ file_upload = gr.File(label="πŸ“‚ Load JSON", file_types=[".json"])
84
 
85
+ export_output = gr.Code(language="json", label="Workflow Configuration", lines=12)
 
 
 
 
 
 
86
 
87
+ # ─── 이벀트 바인딩 ───
88
+ btn_preview.click(
89
+ fn=export_pretty,
90
+ inputs=workflow_builder,
91
+ outputs=export_output
92
+ )
93
+ btn_download.click(
94
+ fn=export_file,
95
+ inputs=workflow_builder
96
+ )
97
+ file_upload.change(
98
+ fn=import_workflow,
99
+ inputs=file_upload,
100
+ outputs=[workflow_builder, export_output]
101
+ )
102
+
103
+ # (선택) 간단 μ‚¬μš©λ²•
104
+ with gr.Accordion("πŸ“– How to Use", open=False):
105
+ gr.Markdown(
106
+ """
107
+ 1. **Add Components** – drag items from the left sidebar onto the canvas
108
+ 2. **Connect Nodes** – drag from blue circle (output) ➜ gray circle (input)
109
+ 3. **Edit Properties** – click a node to change its settings
110
+ 4. **Save** – *Save JSON file* to download your workflow
111
+ 5. **Load** – *Load JSON* to restore (nodes auto-position)
112
+ """
113
+ )
114
 
115
+ # -------------------------------------------------------------------
116
+ # πŸš€ μ‹€ν–‰
117
+ # -------------------------------------------------------------------
118
  if __name__ == "__main__":
119
+ demo.launch(server_name="0.0.0.0", show_error=True)