Kshitij-verbaflo commited on
Commit
68a86d3
Β·
verified Β·
1 Parent(s): f046f61

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +73 -16
app.py CHANGED
@@ -12,18 +12,38 @@ def render_mermaid(jinja_text, variables):
12
  except Exception as e:
13
  return f"**Template Error:** {str(e)}"
14
 
15
- # Add a new variable to the dictionary
16
  def add_variable(key, value, current_vars):
17
  if not key:
18
  return current_vars, "**Variable name is required**"
19
-
 
20
  try:
21
  parsed_value = json.loads(value)
22
  except json.JSONDecodeError:
23
  parsed_value = value
 
 
24
 
 
 
 
 
 
 
 
 
 
 
25
  current_vars[key] = parsed_value
26
- return current_vars, ""
 
 
 
 
 
 
 
27
 
28
  # Reset all variables
29
  def reset_variables():
@@ -73,25 +93,30 @@ with gr.Blocks(css=custom_css) as demo:
73
  elem_id="jinja-code-box"
74
  )
75
 
76
- gr.Markdown("### βž• Add Template Variables")
77
  with gr.Row():
78
- var_key = gr.Textbox(label="πŸ†” Variable Name (key)", placeholder="e.g., user_city")
79
- var_value = gr.Textbox(label="πŸ“¦ Value (JSON)", placeholder='e.g., "London", 42, true')
80
-
81
- with gr.Row():
82
- add_btn = gr.Button("βž• Add Variable")
83
- reset_btn = gr.Button("πŸ” Reset Variables")
84
-
85
- error_box = gr.Markdown("", visible=False)
86
- rendered_vars = gr.JSON(label="πŸ“‹ Current Variables")
87
- variables_state = gr.State({}) # internal variable dictionary
 
 
 
 
 
88
 
89
  # RIGHT COLUMN
90
  with gr.Column(scale=5):
91
  gr.Markdown("### πŸ“ˆ Live Mermaid Preview")
92
  rendered_md = gr.Markdown(elem_classes="markdown-preview")
93
 
94
- # ADD VARIABLE WORKFLOW
95
  add_btn.click(
96
  fn=add_variable,
97
  inputs=[var_key, var_value, variables_state],
@@ -107,6 +132,38 @@ with gr.Blocks(css=custom_css) as demo:
107
  outputs=rendered_md
108
  )
109
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
110
  # RESET BUTTON WORKFLOW
111
  reset_btn.click(
112
  fn=reset_variables,
@@ -128,4 +185,4 @@ with gr.Blocks(css=custom_css) as demo:
128
  # On load
129
  demo.load(render_mermaid, inputs=[jinja_input, variables_state], outputs=rendered_md)
130
 
131
- demo.launch()
 
12
  except Exception as e:
13
  return f"**Template Error:** {str(e)}"
14
 
15
+ # Add variable
16
  def add_variable(key, value, current_vars):
17
  if not key:
18
  return current_vars, "**Variable name is required**"
19
+ if key in current_vars:
20
+ return current_vars, f"⚠️ Variable '{key}' already exists. Use update instead."
21
  try:
22
  parsed_value = json.loads(value)
23
  except json.JSONDecodeError:
24
  parsed_value = value
25
+ current_vars[key] = parsed_value
26
+ return current_vars, f"βœ… Variable '{key}' added"
27
 
28
+ # Update variable
29
+ def update_variable(key, value, current_vars):
30
+ if not key:
31
+ return current_vars, "**Variable name is required**"
32
+ if key not in current_vars:
33
+ return current_vars, f"⚠️ Variable '{key}' does not exist. Use add instead."
34
+ try:
35
+ parsed_value = json.loads(value)
36
+ except json.JSONDecodeError:
37
+ parsed_value = value
38
  current_vars[key] = parsed_value
39
+ return current_vars, f"♻️ Variable '{key}' updated"
40
+
41
+ # Delete variable
42
+ def delete_variable(key, current_vars):
43
+ if key in current_vars:
44
+ del current_vars[key]
45
+ return current_vars, f"πŸ—‘οΈ Variable '{key}' deleted"
46
+ return current_vars, f"⚠️ Variable '{key}' not found"
47
 
48
  # Reset all variables
49
  def reset_variables():
 
93
  elem_id="jinja-code-box"
94
  )
95
 
96
+ gr.Markdown("### βž• Add / ♻️ Update / πŸ—‘οΈ Delete Template Variables")
97
  with gr.Row():
98
+ with gr.Column(scale=2):
99
+ var_key = gr.Textbox(label="πŸ†” Variable Name", placeholder="e.g., user_city")
100
+ var_value = gr.Textbox(label="πŸ“¦ Value (JSON)", placeholder='e.g., "London"')
101
+
102
+ with gr.Row():
103
+ add_btn = gr.Button("βž• Add Variable")
104
+ update_btn = gr.Button("♻️ Update Variable")
105
+ with gr.Row():
106
+ delete_btn = gr.Button("πŸ—‘οΈ Delete Variable")
107
+ reset_btn = gr.Button("πŸ” Reset Variables")
108
+
109
+ with gr.Column(scale=2):
110
+ error_box = gr.Markdown("", visible=False)
111
+ rendered_vars = gr.JSON(label="πŸ“‹ Current Variables")
112
+ variables_state = gr.State({}) # internal variable dictionary
113
 
114
  # RIGHT COLUMN
115
  with gr.Column(scale=5):
116
  gr.Markdown("### πŸ“ˆ Live Mermaid Preview")
117
  rendered_md = gr.Markdown(elem_classes="markdown-preview")
118
 
119
+ # ADD WORKFLOW
120
  add_btn.click(
121
  fn=add_variable,
122
  inputs=[var_key, var_value, variables_state],
 
132
  outputs=rendered_md
133
  )
134
 
135
+ # UPDATE WORKFLOW
136
+ update_btn.click(
137
+ fn=update_variable,
138
+ inputs=[var_key, var_value, variables_state],
139
+ outputs=[variables_state, error_box],
140
+ show_progress=False
141
+ ).then(
142
+ fn=lambda x: x,
143
+ inputs=variables_state,
144
+ outputs=rendered_vars
145
+ ).then(
146
+ fn=render_mermaid,
147
+ inputs=[jinja_input, variables_state],
148
+ outputs=rendered_md
149
+ )
150
+
151
+ # DELETE WORKFLOW
152
+ delete_btn.click(
153
+ fn=delete_variable,
154
+ inputs=[var_key, variables_state],
155
+ outputs=[variables_state, error_box],
156
+ show_progress=False
157
+ ).then(
158
+ fn=lambda x: x,
159
+ inputs=variables_state,
160
+ outputs=rendered_vars
161
+ ).then(
162
+ fn=render_mermaid,
163
+ inputs=[jinja_input, variables_state],
164
+ outputs=rendered_md
165
+ )
166
+
167
  # RESET BUTTON WORKFLOW
168
  reset_btn.click(
169
  fn=reset_variables,
 
185
  # On load
186
  demo.load(render_mermaid, inputs=[jinja_input, variables_state], outputs=rendered_md)
187
 
188
+ demo.launch()