def handle_save_request(self, ws): """Save serialized version of report deltas to the cloud.""" @tornado.gen.coroutine def progress(percent): progress_msg = protobuf.ForwardMsg() progress_msg.upload_report_progress = percent yield ws.write_message(progress_msg.SerializeToString(), binary=True) # Indicate that the save is starting. try: yield progress(0) url = yield self._save_final_report(progress) # Indicate that the save is done. progress_msg = protobuf.ForwardMsg() progress_msg.report_uploaded = url yield ws.write_message(progress_msg.SerializeToString(), binary=True) except Exception as e: # Horrible hack to show something if something breaks. err_msg = '%s: %s' % (type(e).__name__, str(e) or 'No further details.') progress_msg = protobuf.ForwardMsg() progress_msg.report_uploaded = err_msg yield ws.write_message(progress_msg.SerializeToString(), binary=True) raise e
def _maybe_enqueue_initialize_message(self): if self._sent_initialize_message: return self._sent_initialize_message = True msg = protobuf.ForwardMsg() imsg = msg.initialize imsg.sharing_enabled = (config.get_option('global.sharingMode') != 'off') LOGGER.debug('New browser connection: sharing_enabled=%s', msg.initialize.sharing_enabled) imsg.gather_usage_stats = ( config.get_option('browser.gatherUsageStats')) LOGGER.debug('New browser connection: gather_usage_stats=%s', msg.initialize.gather_usage_stats) imsg.streamlit_version = __version__ imsg.session_state.run_on_save = self._scriptrunner.run_on_save imsg.session_state.report_is_running = self._scriptrunner.is_running() imsg.user_info.installation_id = __installation_id__ imsg.user_info.email = Credentials.get_current().activation.email self.enqueue(msg)
def _enqueue_new_report_message(self): self._report.generate_new_id() msg = protobuf.ForwardMsg() msg.new_report.id = self._report.report_id msg.new_report.command_line.extend(self._report.argv) msg.new_report.name = self._report.name self.enqueue(msg)
def _on_script_compile_error(self, exc): """Handles exceptions caught by ScriptRunner during script compilation. We deliver these exceptions to the client via SessionEvent messages. "Normal" exceptions that are thrown during script execution show up as inline elements in the report, but compilation exceptions are handled specially, so that the frontend can leave the previous report up. """ from streamlit.elements import exception_proto msg = protobuf.ForwardMsg() exception_proto.marshall( msg.session_event.script_compilation_exception, exc) self.enqueue(msg)
def enqueue_exception(self, e): """Enqueues an Exception message. Parameters ---------- e : BaseException """ # This does a few things: # 1) Clears the current report in the browser. # 2) Marks the current report as "stopped" in the browser. # 3) HACK: Resets any script params that may have been broken (e.g. the # command-line when rerunning with wrong argv[0]) self._enqueue_script_state_changed_message(ScriptState.STOPPED) self._enqueue_script_state_changed_message(ScriptState.RUNNING) self._enqueue_script_state_changed_message(ScriptState.STOPPED) msg = protobuf.ForwardMsg() msg.delta.id = 0 exception_proto.marshall(msg.delta.new_element.exception, e) self.enqueue(msg)
def enqueue(self, msg): """Add message into queue, possibly composing it with another message. Parameters ---------- msg : protobuf.ForwardMsg """ with self._lock: if not msg.HasField('delta'): self._queue.append(msg) elif msg.delta.id in self._delta_id_map: # Combine the previous message into the new messages. index = self._delta_id_map[msg.delta.id] old_msg = self._queue[index] composed_delta = compose_deltas(old_msg.delta, msg.delta) new_msg = protobuf.ForwardMsg() new_msg.delta.CopyFrom(composed_delta) self._queue[index] = new_msg else: self._delta_id_map[msg.delta.id] = len(self._queue) self._queue.append(msg)
# Copyright 2019 Streamlit Inc. All rights reserved. """Unit test of ReportQueue.py.""" import unittest from streamlit.ReportQueue import ReportQueue from streamlit.elements import data_frame_proto import streamlit.protobuf as protobuf # For the messages below, we don't really care about their contents so much as # their general type. INIT_MSG = protobuf.ForwardMsg() INIT_MSG.initialize.sharing_enabled = True TEXT_DELTA_MSG1 = protobuf.ForwardMsg() TEXT_DELTA_MSG1.delta.new_element.text.body = 'text1' TEXT_DELTA_MSG2 = protobuf.ForwardMsg() TEXT_DELTA_MSG2.delta.new_element.text.body = 'text2' DF_DELTA_MSG = protobuf.ForwardMsg() data_frame_proto.marshall_data_frame({ 'col1': [0, 1, 2], 'col2': [10, 11, 12] }, DF_DELTA_MSG.delta.new_element.data_frame) ADD_ROWS_MSG = protobuf.ForwardMsg() data_frame_proto.marshall_data_frame({ 'col1': [3, 4, 5], 'col2': [13, 14, 15]
def progress(percent): progress_msg = protobuf.ForwardMsg() progress_msg.upload_report_progress = percent yield ws.write_message(progress_msg.SerializeToString(), binary=True)
def _enqueue_report_finished_message(self): msg = protobuf.ForwardMsg() msg.report_finished = True self.enqueue(msg)
def _enqueue_file_change_message(self, _): LOGGER.debug('Enqueuing report_changed message (id=%s)', self.id) msg = protobuf.ForwardMsg() msg.session_event.report_changed_on_disk = True self.enqueue(msg)
def _enqueue_session_state_changed_message(self): msg = protobuf.ForwardMsg() msg.session_state_changed.run_on_save = self._scriptrunner.run_on_save msg.session_state_changed.report_is_running = \ self._scriptrunner.is_running() self.enqueue(msg)