Skidl / app.py
Arrcttacsrks's picture
Create app.py
a715790 verified
raw
history blame
1.47 kB
# file: app.py
import gradio as gr
from skidl import *
import tempfile
import os
def generate_circuit(skidl_code):
# Reset SKiDL trước mỗi lần build
reset()
try:
# Lưu code tạm thời
with tempfile.NamedTemporaryFile(delete=False, suffix='.py') as tmp:
tmp.write(skidl_code.encode('utf-8'))
tmp_path = tmp.name
# Thực thi code
namespace = {}
exec(open(tmp_path).read(), namespace)
# Generate netlist
netlist_str = generate_netlist()
# Lưu netlist thành file cho phép tải
with tempfile.NamedTemporaryFile(delete=False, suffix='.net', mode='w') as out_file:
out_file.write(netlist_str)
netlist_file_path = out_file.name
return "Netlist Generated Successfully!", netlist_file_path
except Exception as e:
return f"Error: {e}", None
finally:
if os.path.exists(tmp_path):
os.remove(tmp_path)
# Gradio UI
with gr.Blocks() as demo:
gr.Markdown("# SKiDL Circuit Builder")
skidl_input = gr.Textbox(label="Enter SKiDL Python Code", lines=20)
output_message = gr.Textbox(label="Status / Error", interactive=False)
download_btn = gr.File(label="Download Netlist")
run_button = gr.Button("Build Circuit")
run_button.click(fn=generate_circuit, inputs=[skidl_input], outputs=[output_message, download_btn])
if __name__ == "__main__":
demo.launch()