from flask import Flask, render_template, request, redirect import yaml import ast app = Flask(__name__) key_type_map = { 'obs_type': str, 'obs_type_code': str, 'obs_id': str, 'run_chips': list, 'call_sequence': { 'scie_obs': { 'shutter_effect': bool, 'flat_fielding': bool, 'field_dist': bool, }, 'sky_background': { 'shutter_effect': bool, 'flat_fielding': bool, 'enable_straylight_model': bool, 'flat_level': None, 'flat_level_filt': None, }, 'PRNU_effect': {}, 'cosmic_rays': { 'save_cosmic_img': bool, }, 'poisson_and_dark': { 'add_dark': bool, }, 'bright_fatter': {}, 'detector_defects': { 'hot_pixels': bool, 'dead_pixels': bool, 'bad_columns': bool, }, 'nonlinearity': {}, 'blooming': {}, 'prescan_overscan': { 'add_dark': bool, }, 'bias': { 'bias_16channel': bool, }, 'readout_noise': {}, 'gain': { 'gain_16channel': bool, }, 'quantization_and_output': { 'format_output': bool, }, }, } def convert_dict_values(d, key_type_map): for key, value in d.items(): if isinstance(value, dict): convert_dict_values(value, key_type_map[key]) elif key in key_type_map: if key_type_map[key] is int: d[key] = int(value) if key_type_map[key] is float: d[key] = float(value) if key_type_map[key] is bool: if d[key].lower() == 'yes' or d[key].lower() == 'true': d[key] = True else: d[key] = False if key_type_map[key] is str: d[key] = str(value) if key_type_map[key] is list: d[key] = ast.literal_eval(value) if key_type_map[key] is None: d[key] = None def load_yaml(): with open('templates/obs_config_SCI.yaml', 'r') as file: return yaml.safe_load(file) def save_yaml(data): convert_dict_values(data, key_type_map) with open('config_reset/obs_config_SCI_reset.yaml', 'w') as file: yaml.dump(data, file, default_flow_style=False, sort_keys=False) def render_form(data, parent_key=''): form_html = '' for key, value in data.items(): full_key = f"{parent_key}.{key}" if parent_key else key if isinstance(value, dict): # 处理字典 form_html += f"

{key}

{render_form(value, full_key)}
" else: form_html += f"" form_html += f"
" return form_html @app.route('/', methods=['GET', 'POST']) def index(): if request.method == 'POST': data = load_yaml() for key in request.form: keys = key.split('.') temp = data for k in keys[:-1]: temp = temp[k] temp[keys[-1]] = request.form[key] save_yaml(data) return redirect('/') data = load_yaml() form_html = render_form(data) return render_template('index_obs.html', form_html=form_html) if __name__ == '__main__': app.run(debug=True)