File size: 5,320 Bytes
1f1f0c9
 
f046f61
1f1f0c9
f046f61
1f1f0c9
 
f046f61
1f1f0c9
 
 
 
 
f046f61
 
 
68a86d3
 
f046f61
 
 
 
68a86d3
 
f046f61
68a86d3
 
 
 
 
 
 
 
 
f046f61
68a86d3
 
 
 
 
 
 
f046f61
 
 
 
1f1f0c9
 
daf4783
 
 
1f1f0c9
 
 
 
 
f046f61
 
 
 
 
1f1f0c9
 
f046f61
 
 
1f1f0c9
f046f61
 
 
1f1f0c9
f046f61
1f1f0c9
 
f046f61
 
1f1f0c9
 
f046f61
1f1f0c9
 
 
 
f046f61
 
1f1f0c9
 
68a86d3
f046f61
68a86d3
 
 
 
 
 
 
 
 
 
 
 
 
 
daf4783
f046f61
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
68a86d3
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
f046f61
 
 
 
 
 
 
 
 
 
 
 
 
1f1f0c9
f046f61
1f1f0c9
f046f61
1f1f0c9
68a86d3
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
import gradio as gr
from jinja2 import Template
import json

def render_mermaid(jinja_text, variables):
    try:
        template = Template(jinja_text)
        rendered_code = template.render(**variables)
        mermaid_md = f"```mermaid\n{rendered_code}\n```"
        return mermaid_md
    except Exception as e:
        return f"**Template Error:** {str(e)}"

def add_variable(key, value, current_vars):
    if not key:
        return current_vars, "**Variable name is required**"
    if key in current_vars:
        return current_vars, f"⚠️ Variable '{key}' already exists. Use update instead."
    try:
        parsed_value = json.loads(value)
    except json.JSONDecodeError:
        parsed_value = value
    current_vars[key] = parsed_value
    return current_vars, f"βœ… Variable '{key}' added"

def update_variable(key, value, current_vars):
    if not key:
        return current_vars, "**Variable name is required**"
    if key not in current_vars:
        return current_vars, f"⚠️ Variable '{key}' does not exist. Use add instead."
    try:
        parsed_value = json.loads(value)
    except json.JSONDecodeError:
        parsed_value = value
    current_vars[key] = parsed_value
    return current_vars, f"♻️ Variable '{key}' updated"

def delete_variable(key, current_vars):
    if key in current_vars:
        del current_vars[key]
        return current_vars, f"πŸ—‘οΈ Variable '{key}' deleted"
    return current_vars, f"⚠️ Variable '{key}' not found"

def reset_variables():
    return {}

default_template = """
flowchart TD
    Start(["Start"]) --> A{"Is city known?"}
    A -->|Yes| B["You are in {{ user_city }}"]
    A -->|No| C["Please choose a city"]
"""

custom_css = """
.gr-box { padding: 1.5rem; }

#jinja-code-box {
    max-height: 400px;
    overflow-y: auto;
    border: 1px solid #ddd;
    border-radius: 6px;
}

.markdown-preview {
    padding: 1rem;
    max-height: 85vh;
    overflow-y: auto;
    white-space: pre-wrap;
    font-size: 0.95rem;
    line-height: 1.5;
}
.gr-button { margin-right: 0.5rem; }
"""

with gr.Blocks(css=custom_css) as demo:
    gr.Markdown("## 🧠 Jinja2 β†’ Mermaid Renderer (Dynamic Variables - Interactive)")

    with gr.Row():
        with gr.Column(scale=7):
            jinja_input = gr.Code(
                label="✍️ Jinja2 + Mermaid Template",
                language="html",
                value=default_template,
                lines=18,
                elem_id="jinja-code-box"
            )

            gr.Markdown("### βž• Add / ♻️ Update / πŸ—‘οΈ Delete Template Variables")
            with gr.Row():
                with gr.Column(scale=2):
                    var_key = gr.Textbox(label="πŸ†” Variable Name", placeholder="e.g., user_city")
                    var_value = gr.Textbox(label="πŸ“¦ Value (JSON)", placeholder='e.g., "London"')
                    
                    with gr.Row():
                        add_btn = gr.Button("βž• Add Variable")
                        update_btn = gr.Button("♻️ Update Variable")
                    with gr.Row():
                        delete_btn = gr.Button("πŸ—‘οΈ Delete Variable")
                        reset_btn = gr.Button("πŸ” Reset Variables")

                with gr.Column(scale=2):
                    error_box = gr.Markdown("", visible=False)
                    rendered_vars = gr.JSON(label="πŸ“‹ Current Variables")
                    variables_state = gr.State({})

        with gr.Column(scale=5):
            gr.Markdown("### πŸ“ˆ Live Mermaid Preview")
            rendered_md = gr.Markdown(elem_classes="markdown-preview")

    add_btn.click(
        fn=add_variable,
        inputs=[var_key, var_value, variables_state],
        outputs=[variables_state, error_box],
        show_progress=False
    ).then(
        fn=lambda x: x,
        inputs=variables_state,
        outputs=rendered_vars
    ).then(
        fn=render_mermaid,
        inputs=[jinja_input, variables_state],
        outputs=rendered_md
    )

    update_btn.click(
        fn=update_variable,
        inputs=[var_key, var_value, variables_state],
        outputs=[variables_state, error_box],
        show_progress=False
    ).then(
        fn=lambda x: x,
        inputs=variables_state,
        outputs=rendered_vars
    ).then(
        fn=render_mermaid,
        inputs=[jinja_input, variables_state],
        outputs=rendered_md
    )

    delete_btn.click(
        fn=delete_variable,
        inputs=[var_key, variables_state],
        outputs=[variables_state, error_box],
        show_progress=False
    ).then(
        fn=lambda x: x,
        inputs=variables_state,
        outputs=rendered_vars
    ).then(
        fn=render_mermaid,
        inputs=[jinja_input, variables_state],
        outputs=rendered_md
    )

    reset_btn.click(
        fn=reset_variables,
        inputs=[],
        outputs=variables_state
    ).then(
        fn=lambda x: x,
        inputs=variables_state,
        outputs=rendered_vars
    ).then(
        fn=render_mermaid,
        inputs=[jinja_input, variables_state],
        outputs=rendered_md
    )

    jinja_input.change(render_mermaid, inputs=[jinja_input, variables_state], outputs=rendered_md)

    demo.load(render_mermaid, inputs=[jinja_input, variables_state], outputs=rendered_md)

demo.launch()