def on_text_changed(buf: Buffer, state=state): global text_area filename = buf.text.rstrip().rsplit("\n", 1)[-1].strip() if filename == "": return if state["filename"] != filename: state["filename"] = filename try: with open(filename) as rf: state["content"] = rf.read() text_area.buffer.reset(Document(state["content"])) buf.validation_state = ValidationState.VALID buf.validation_error = None buf.complete_state = None except Exception as e: buf.validation_error = ValidationError(message=repr(e)) buf.validation_state = ValidationState.INVALID