nicoaspra/G-code_Programming_Assistant
1
1# app.py2 3import streamlit as st4import pandas as pd5from streamlit_ace import st_ace6from python_gcode_checker import run_checks7from settings_report_details import generate_detailed_report8 9def analyze_gcode(gcode, depth_max=0.1):10 errors, warnings = run_checks(gcode, depth_max)11 config_report = generate_detailed_report(gcode)12 return errors, warnings, config_report13 14def format_issues(issues):15 formatted = []16 for line_num, message in issues:17 if line_num > 0:18 formatted.append(f"Line {line_num}: {message}")19 else:20 formatted.append(message) # No "Line 0" for general warnings/errors21 return formatted22 23def parse_config_report(config_report):24 config_lines = config_report.strip().split('\n')25 config_data = {"Setting": [], "Value": []}26 for line in config_lines:27 if ':' in line:28 key, value = line.split(':', 1)29 config_data["Setting"].append(key.strip())30 config_data["Value"].append(value.strip())31 return pd.DataFrame(config_data)32 33st.title("G-code Programming Assistant (v0.1)")34 35st.markdown("""36Welcome to the G-code Assistant! This tool helps you verify and analyze your G-code for potential errors and warnings before running it on your machine.37 38**Note:** This tool is limited to simple carving operations on a CNC milling machine.39 40This is a beta version, and you're free to use it for checking your G-codes. If you encounter any issues or unexpected behavior, please contact the developer at **nico.aspra@bicol-u.edu.ph**.41""")42 43depth_max = st.number_input("Maximum Depth of Cut", min_value=0.0, value=0.1, step=0.1, format="%.1f")44 45# Use Ace editor for G-code input with line numbering46gcode_input = st_ace(language='text', theme='cobalt', placeholder="Enter G-code here...", font_size=14, height=300)47 48if st.button("Analyze G-code"):49 errors, warnings, config_report = analyze_gcode(gcode_input, depth_max)50 51 st.subheader(f"Errors ({len(errors)})")52 if errors:53 for message in format_issues(errors):54 st.error(message)55 else:56 st.success("No errors found.")57 58 st.subheader(f"Warnings ({len(warnings)})")59 if warnings:60 for message in format_issues(warnings):61 st.warning(message)62 else:63 st.info("No warnings found.")64 65 st.subheader("Configuration Settings")66 config_df = parse_config_report(config_report)67 st.table(config_df)68 69 # # Embed NC Viewer for G-code visualization70 # st.subheader("G-code Visualization (NC Viewer)")71 # gcode_query_param = st.query_params.get("gcode", gcode_input) # Replace deprecated method72 # ncviewer_url = f"https://ncviewer.com/?code={gcode_query_param}"73 # st.markdown(74 # f'<iframe src="{ncviewer_url}" width="100%" height="600" frameborder="0"></iframe>',75 # unsafe_allow_html=True76 # )77 78st.markdown("""79---80<div style="text-align: center; font-size: small;">81 Developed by <strong>Aspra, N.</strong> | © 2024 All Rights Reserved82</div>83""", unsafe_allow_html=True)