Spaces:
Sleeping
Sleeping
Update app.py
Browse files
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
|
| 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 |
-
|
| 79 |
-
|
| 80 |
-
|
| 81 |
-
|
| 82 |
-
|
| 83 |
-
|
| 84 |
-
|
| 85 |
-
|
| 86 |
-
|
| 87 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 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
|
| 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()
|