Spaces:
Runtime error
Runtime error
1littlecoder
commited on
Commit
•
fb3ff6c
1
Parent(s):
ee389b3
Update app.py
Browse files
app.py
CHANGED
@@ -1,60 +1,27 @@
|
|
1 |
import gradio as gr
|
2 |
import base64
|
3 |
-
from mermaid import Mermaid
|
4 |
-
from mermaid.graph import Graph
|
5 |
-
|
6 |
-
# HTML template for rendering Mermaid diagrams
|
7 |
-
html_template = """
|
8 |
-
<!DOCTYPE html>
|
9 |
-
<html>
|
10 |
-
<head>
|
11 |
-
<script src="https://cdn.jsdelivr.net/npm/mermaid/dist/mermaid.min.js"></script>
|
12 |
-
<style>
|
13 |
-
.mermaid {{
|
14 |
-
display: block;
|
15 |
-
margin: auto;
|
16 |
-
}}
|
17 |
-
</style>
|
18 |
-
</head>
|
19 |
-
<body>
|
20 |
-
<div class="mermaid">{}</div>
|
21 |
-
<script>
|
22 |
-
mermaid.initialize({{ startOnLoad: true }});
|
23 |
-
</script>
|
24 |
-
</body>
|
25 |
-
</html>
|
26 |
-
"""
|
27 |
|
28 |
def render_mermaid(mermaid_code):
|
29 |
-
# Create
|
30 |
-
sequence = Graph('Sequence-diagram', mermaid_code)
|
31 |
-
render = Mermaid(sequence)
|
32 |
-
|
33 |
-
# Create HTML output
|
34 |
-
html_content = html_template.format(render)
|
35 |
-
|
36 |
-
# Create PNG output
|
37 |
graphbytes = mermaid_code.encode("utf8")
|
38 |
base64_bytes = base64.urlsafe_b64encode(graphbytes)
|
39 |
base64_string = base64_bytes.decode("ascii")
|
40 |
png_url = "https://mermaid.ink/img/" + base64_string
|
41 |
|
42 |
-
return
|
43 |
|
44 |
-
# Create a Gradio Blocks interface with Citrus theme
|
45 |
with gr.Blocks(theme=gr.themes.Citrus()) as mrender:
|
46 |
gr.Markdown("# Mermaid Diagram Renderer")
|
47 |
-
gr.Markdown("Input your Mermaid diagram code to
|
48 |
|
49 |
with gr.Row():
|
50 |
with gr.Column():
|
51 |
mermaid_input = gr.Textbox(label="Mermaid Code", placeholder="Enter your Mermaid diagram code here...")
|
52 |
submit_btn = gr.Button("Render")
|
53 |
with gr.Column():
|
54 |
-
html_output = gr.HTML()
|
55 |
image_output = gr.Image()
|
56 |
|
57 |
-
submit_btn.click(fn=render_mermaid, inputs=mermaid_input, outputs=
|
|
|
58 |
|
59 |
-
# Launch the app
|
60 |
mrender.launch()
|
|
|
1 |
import gradio as gr
|
2 |
import base64
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
3 |
|
4 |
def render_mermaid(mermaid_code):
|
5 |
+
# Create PNG output from the Mermaid code
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
6 |
graphbytes = mermaid_code.encode("utf8")
|
7 |
base64_bytes = base64.urlsafe_b64encode(graphbytes)
|
8 |
base64_string = base64_bytes.decode("ascii")
|
9 |
png_url = "https://mermaid.ink/img/" + base64_string
|
10 |
|
11 |
+
return png_url
|
12 |
|
|
|
13 |
with gr.Blocks(theme=gr.themes.Citrus()) as mrender:
|
14 |
gr.Markdown("# Mermaid Diagram Renderer")
|
15 |
+
gr.Markdown("Input your Mermaid diagram code to generate a PNG image.")
|
16 |
|
17 |
with gr.Row():
|
18 |
with gr.Column():
|
19 |
mermaid_input = gr.Textbox(label="Mermaid Code", placeholder="Enter your Mermaid diagram code here...")
|
20 |
submit_btn = gr.Button("Render")
|
21 |
with gr.Column():
|
|
|
22 |
image_output = gr.Image()
|
23 |
|
24 |
+
submit_btn.click(fn=render_mermaid, inputs=mermaid_input, outputs=image_output)
|
25 |
+
|
26 |
|
|
|
27 |
mrender.launch()
|