-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.py
61 lines (46 loc) · 1.86 KB
/
app.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
import streamlit as st
from story_content import get_story_content # Import the function
# Initialize 'current_node' in session_state if it doesn't exist
def initialize_session_state():
if 'current_node' not in st.session_state:
st.session_state['current_node'] = '0'
# Call the function to ensure the session state is initialized
initialize_session_state()
# Function to display the story node. Refactored for better readability.
def display_story_node(node_id):
story_content = get_story_content() # Call the function to get story content
node = story_content.get(node_id)
if node:
display_node_content(node)
display_node_choices(node, node_id)
else:
display_error_and_restart_option()
# Function to display node content
def display_node_content(node):
if 'image' in node and node['image']:
st.image(node['image'], width=400)
st.write(node['text'])
# Function to display choices and handle button clicks
def display_node_choices(node, node_id):
for choice_text, next_node in node.get('choices', []):
if st.button(choice_text):
st.session_state['current_node'] = next_node
st.experimental_rerun()
return
if node_id != '0': # Avoid showing restart on the initial node
display_restart_option()
# Display an error message and an option to restart the story
def display_error_and_restart_option():
st.error("The story node is missing or the story has ended. Choose a new path or restart the game.")
display_restart_option()
# Display a restart button
def display_restart_option():
if st.button('Restart the Story'):
st.session_state['current_node'] = '0'
st.experimental_rerun()
# Main program flow
def main():
initialize_session_state()
display_story_node(st.session_state['current_node'])
if __name__ == "__main__":
main()