liveBook · Manning

Machine state

As explained in Build Python Web Apps with Streamlit

A game state is a distinct phase of a game that determines the actions and interface elements currently applicable.

Machine state records which phase an application is in, so each phase can perform its own actions and show the appropriate interface. In a sequential game, states prevent unrelated operations from running and help ensure each language-model request happens once per question. section 9.4

A question-and-answer game can move through four states:

  • GET_QUESTION retrieves a question and displays a waiting indicator. section 9.4
  • ASK_QUESTION displays the question and accepts the player's answer. section 9.4
  • EVALUATE_ANSWER sends the answer for evaluation and displays progress. section 9.4
  • STATE_RESULT reports whether the answer was correct and may reveal the correct answer. section 9.4

A status-controlled Streamlit game

Store status on a central Game object, then let methods change it: obtain_question moves to ASK_QUESTION, accept_answer moves to EVALUATE_ANSWER, and evaluation moves to STATE_RESULT. The frontend branches on game.status, renders the matching widgets, and calls st.rerun() after transitions. section 9.4

Figure 9.7 Conditional branching in the frontend based on the game’s state

Use the state-branching diagram to connect each status with its corresponding frontend display. section 9.4

How Build Python Web Apps with Streamlit covers it

How deep it goes
Defines four states and demonstrates status attributes, transition methods, conditional rendering, and reruns.
Examples
Uses a Fact Frenzy question flow with Game.status, answer input, spinners, result feedback, and st.rerun().
What it assumes
Assumes basic application and frontend programming while retaining values through Streamlit session state.
What you can do after
Build it from scratch by modeling application phases, changing status through methods, and rendering each phase conditionally.
How it connects
Connects machine state to session state, conditional logic, frontend widgets, and language-model requests.

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Build Python Web Apps with Streamlit. The section links point to the parts of the book it draws on.