def run_labeler(): state = SessionState.get(vnum=0, vlab=0) vlist = get_video_list() st.title("Emotion Labeler") st.write( "Please label as many videos as you can. When done, simply close browser tab." ) st.write("") st.write(f"Total videos labeled in current session:{state.vlab}") st.write("Note: refreshing browser tab will reset counts.") vrnd, choices = get_random_video(vlist, state.vnum) vpath = f'{bucket_path}vid_{vrnd}.mp4' with open(vpath, 'rb') as video_file: vbytes = video_file.read() st.video(vbytes) labeled = False emo = st.selectbox('Emotion:', choices) if emo[:6] != "Select": labeled = True if st.button('Get next video'): if labeled: state.vlab += 1 with open(labelfile, 'a') as fd: fd.write( f"{time.time()}, {SessionState.get_session_id()}, {vrnd}, {emo}\n" ) state.vnum += 1 raise RerunException(RerunData(widget_state=None))
def main(): df = read_jarchive() state = SessionState.get(question_number=1, num_correct=0, score=0) st.title("Streamlit Jeopardy!") category, question, answer, value = get_one_question( state.question_number, df) answered = False st.write(f"Question from category {category} for ${value}:") st.write(f" {question}") response = st.text_input("What is: ", key=str(state.question_number)) if (response != ''): sresponse = sanitize(response) sanswer = sanitize(answer) if (compare_strings(sresponse, sanswer) >= 0.5): answered = True st.write(f"Correct! The reference answer is {answer}.") else: answered = False st.write( f"Sorry! Your response was {response}. The correct answer is {answer}." ) if (answered): state.num_correct += 1 state.score += value else: state.score -= value st.write( f"Your score is {state.num_correct}/{state.question_number} and winnings are ${state.score}" ) st.write("") if st.button('Next question'): state.question_number += 1 raise RerunException(RerunData(widget_state=None)) if st.button('Reset score'): state.question_number = 0 state.num_correct = 0 state.score = 0 raise RerunException(RerunData(widget_state=None))
def request_rerun(self, widget_state=None): """Signal that we're interested in running the script. If the script is not already running, it will be started immediately. Otherwise, a rerun will be requested. Parameters ---------- widget_state : dict | None The widget state dictionary to run the script with, or None to use the widget state from the previous run of the script. """ self._enqueue_script_request(ScriptRequest.RERUN, RerunData(widget_state))
def reset_session(session): session.message_value = None raise RerunException(RerunData(widget_state=None))
def enqueue_rerun(self, argv=None, widget_state=None): self.script_request_queue.enqueue( ScriptRequest.RERUN, RerunData(widget_state=widget_state) )
def test_rerun_data_coalescing(self): """Test that multiple RERUN requests get coalesced with expected values. (This is similar to widgets_test.test_coalesce_widget_states - it's testing the same thing, but through the ScriptEventQueue interface.) """ queue = ScriptRequestQueue() states = WidgetStates() _create_widget('trigger', states).trigger_value = True _create_widget('int', states).int_value = 123 queue.enqueue(ScriptRequest.RERUN, RerunData(argv=None, widget_state=states)) states = WidgetStates() _create_widget('trigger', states).trigger_value = False _create_widget('int', states).int_value = 456 queue.enqueue(ScriptRequest.RERUN, RerunData(argv=None, widget_state=states)) event, data = queue.dequeue() self.assertEqual(event, ScriptRequest.RERUN) widgets = Widgets() widgets.set_state(data.widget_state) # Coalesced triggers should be True if either the old or # new value was True self.assertEqual(True, widgets.get_widget_value('trigger')) # Other widgets should have their newest value self.assertEqual(456, widgets.get_widget_value('int')) # We should have no more events self.assertEqual((None, None), queue.dequeue(), 'Expected empty event queue') # Test that we can coalesce if previous widget state is None queue.enqueue(ScriptRequest.RERUN, RerunData(argv=None, widget_state=None)) queue.enqueue(ScriptRequest.RERUN, RerunData(argv=None, widget_state=None)) states = WidgetStates() _create_widget('int', states).int_value = 789 queue.enqueue(ScriptRequest.RERUN, RerunData(argv=None, widget_state=states)) event, data = queue.dequeue() widgets = Widgets() widgets.set_state(data.widget_state) self.assertEqual(event, ScriptRequest.RERUN) self.assertEqual(789, widgets.get_widget_value('int')) # We should have no more events self.assertEqual((None, None), queue.dequeue(), 'Expected empty event queue') # Test that we can coalesce if our *new* widget state is None states = WidgetStates() _create_widget('int', states).int_value = 101112 queue.enqueue(ScriptRequest.RERUN, RerunData(argv=None, widget_state=states)) queue.enqueue(ScriptRequest.RERUN, RerunData(argv=None, widget_state=None)) event, data = queue.dequeue() widgets = Widgets() widgets.set_state(data.widget_state) self.assertEqual(event, ScriptRequest.RERUN) self.assertEqual(101112, widgets.get_widget_value('int')) # We should have no more events self.assertEqual((None, None), queue.dequeue(), 'Expected empty event queue')
def rerun(): """Rerun a Streamlit app from the top!""" widget_states = _get_widget_states() raise RerunException(RerunData(widget_states))
ans = arr[0] * arr[1] choices = ["Please select an answer", ans, ans - 1, ans + 1, ans + 2] return arr, q, ans, choices arr, q, ans, choices = get_question(state.question_number) answered = False st.write(f"Your score is {state.num_correct}/{state.question_number}") st.write("") st.text(f"Solve: {q}") a = st.selectbox('Answer:', choices) if a != "Please select an answer": st.write(f"You chose {a}") if (ans == a): answered = True st.write(f"Correct!") else: st.write(f"Wrong!, the correct answer is {ans}") if st.button('Next question'): state.question_number += 1 if (answered): state.num_correct += 1 raise RerunException(RerunData(widget_state=None)) if st.button('Reset'): state.question_number = 0 state.num_correct = 0 raise RerunException(RerunData(widget_state=None))
def rerun(): """Rerun a Streamlit app from the top! refer to https://gist.github.com/tvst/58c42300d3b6de48e805af7429cac2e7 """ widget_states = _get_widget_states() raise RerunException(RerunData(widget_states))
def rerun(): """Rerun a Streamlit app from the top!""" widget_states = st_session()._widget_states raise RerunException(RerunData(widget_states))