Spaces:
Sleeping
Sleeping
File size: 1,471 Bytes
a715790 |
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 |
# 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() |