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

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +4 -19
app.py CHANGED
@@ -2,7 +2,6 @@ import gradio as gr
2
  from jinja2 import Template
3
  import json
4
 
5
- # Render the Mermaid chart with Jinja variables
6
  def render_mermaid(jinja_text, variables):
7
  try:
8
  template = Template(jinja_text)
@@ -12,7 +11,6 @@ def render_mermaid(jinja_text, variables):
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**"
@@ -25,7 +23,6 @@ def add_variable(key, value, current_vars):
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**"
@@ -38,24 +35,20 @@ def update_variable(key, value, current_vars):
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():
50
  return {}
51
 
52
- # Default Jinja + Mermaid template
53
  default_template = """
54
  flowchart TD
55
- Start(["Start"])
56
- Start --> Decision{"Is city known?"}
57
- Decision -->|Yes| City["You are in {{ user_city }}"]
58
- Decision -->|No| AskCity["Please choose a city"]
59
  """
60
 
61
  custom_css = """
@@ -83,7 +76,6 @@ with gr.Blocks(css=custom_css) as demo:
83
  gr.Markdown("## 🧠 Jinja2 β†’ Mermaid Renderer (Dynamic Variables - Interactive)")
84
 
85
  with gr.Row():
86
- # LEFT COLUMN
87
  with gr.Column(scale=7):
88
  jinja_input = gr.Code(
89
  label="✍️ Jinja2 + Mermaid Template",
@@ -109,14 +101,12 @@ with gr.Blocks(css=custom_css) as demo:
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,7 +122,6 @@ with gr.Blocks(css=custom_css) as demo:
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],
@@ -148,7 +137,6 @@ with gr.Blocks(css=custom_css) as demo:
148
  outputs=rendered_md
149
  )
150
 
151
- # DELETE WORKFLOW
152
  delete_btn.click(
153
  fn=delete_variable,
154
  inputs=[var_key, variables_state],
@@ -164,7 +152,6 @@ with gr.Blocks(css=custom_css) as demo:
164
  outputs=rendered_md
165
  )
166
 
167
- # RESET BUTTON WORKFLOW
168
  reset_btn.click(
169
  fn=reset_variables,
170
  inputs=[],
@@ -179,10 +166,8 @@ with gr.Blocks(css=custom_css) as demo:
179
  outputs=rendered_md
180
  )
181
 
182
- # On template change
183
  jinja_input.change(render_mermaid, inputs=[jinja_input, variables_state], outputs=rendered_md)
184
 
185
- # On load
186
  demo.load(render_mermaid, inputs=[jinja_input, variables_state], outputs=rendered_md)
187
 
188
  demo.launch()
 
2
  from jinja2 import Template
3
  import json
4
 
 
5
  def render_mermaid(jinja_text, variables):
6
  try:
7
  template = Template(jinja_text)
 
11
  except Exception as e:
12
  return f"**Template Error:** {str(e)}"
13
 
 
14
  def add_variable(key, value, current_vars):
15
  if not key:
16
  return current_vars, "**Variable name is required**"
 
23
  current_vars[key] = parsed_value
24
  return current_vars, f"βœ… Variable '{key}' added"
25
 
 
26
  def update_variable(key, value, current_vars):
27
  if not key:
28
  return current_vars, "**Variable name is required**"
 
35
  current_vars[key] = parsed_value
36
  return current_vars, f"♻️ Variable '{key}' updated"
37
 
 
38
  def delete_variable(key, current_vars):
39
  if key in current_vars:
40
  del current_vars[key]
41
  return current_vars, f"πŸ—‘οΈ Variable '{key}' deleted"
42
  return current_vars, f"⚠️ Variable '{key}' not found"
43
 
 
44
  def reset_variables():
45
  return {}
46
 
 
47
  default_template = """
48
  flowchart TD
49
+ Start(["Start"]) --> A{"Is city known?"}
50
+ A -->|Yes| B["You are in {{ user_city }}"]
51
+ A -->|No| C["Please choose a city"]
 
52
  """
53
 
54
  custom_css = """
 
76
  gr.Markdown("## 🧠 Jinja2 β†’ Mermaid Renderer (Dynamic Variables - Interactive)")
77
 
78
  with gr.Row():
 
79
  with gr.Column(scale=7):
80
  jinja_input = gr.Code(
81
  label="✍️ Jinja2 + Mermaid Template",
 
101
  with gr.Column(scale=2):
102
  error_box = gr.Markdown("", visible=False)
103
  rendered_vars = gr.JSON(label="πŸ“‹ Current Variables")
104
+ variables_state = gr.State({})
105
 
 
106
  with gr.Column(scale=5):
107
  gr.Markdown("### πŸ“ˆ Live Mermaid Preview")
108
  rendered_md = gr.Markdown(elem_classes="markdown-preview")
109
 
 
110
  add_btn.click(
111
  fn=add_variable,
112
  inputs=[var_key, var_value, variables_state],
 
122
  outputs=rendered_md
123
  )
124
 
 
125
  update_btn.click(
126
  fn=update_variable,
127
  inputs=[var_key, var_value, variables_state],
 
137
  outputs=rendered_md
138
  )
139
 
 
140
  delete_btn.click(
141
  fn=delete_variable,
142
  inputs=[var_key, variables_state],
 
152
  outputs=rendered_md
153
  )
154
 
 
155
  reset_btn.click(
156
  fn=reset_variables,
157
  inputs=[],
 
166
  outputs=rendered_md
167
  )
168
 
 
169
  jinja_input.change(render_mermaid, inputs=[jinja_input, variables_state], outputs=rendered_md)
170
 
 
171
  demo.load(render_mermaid, inputs=[jinja_input, variables_state], outputs=rendered_md)
172
 
173
  demo.launch()