demo3/visa-scpi-example/server.py

43 lines
1.3 KiB
Python
Raw Normal View History

from flask import Flask, request, jsonify, send_from_directory
from parse_inp import parse_inp_file, update_inp_file, save_comments
import os
app = Flask(__name__)
# 确保在启动时解析inp文件
INP_FILE_PATH = 'visa-scpi-example/File.inp'
@app.route('/')
def index():
commands, comments = parse_inp_file(INP_FILE_PATH)
return send_from_directory(os.path.abspath('visa-scpi-example'), 'index.html')
@app.route('/commands.json')
def get_commands():
return send_from_directory(os.path.abspath('visa-scpi-example'), 'commands.json')
@app.route('/update_command', methods=['POST'])
def update_command():
data = request.json
try:
update_inp_file(INP_FILE_PATH, data['command'], data['value'])
# 更新后重新解析文件
parse_inp_file(INP_FILE_PATH)
return jsonify({'success': True})
except Exception as e:
return jsonify({'success': False, 'error': str(e)}), 500
@app.route('/save_comments', methods=['POST'])
def save_comments_route():
comments = request.json
save_comments(comments)
return jsonify({'success': True})
@app.route('/comments.json')
def get_comments():
return send_from_directory(os.path.abspath('visa-scpi-example'), 'comments.json')
if __name__ == '__main__':
# 启动时先解析一次文件
parse_inp_file(INP_FILE_PATH)
app.run(debug=True)