提交 2096676e 编写于 作者: J jiangtao

Mon Mar 25 18:07:00 CST 2024 inscode

上级 0520122c
import streamlit as st
from streamlit_option_menu import option_menu
import random
import inscode
# 设置Streamlit应用程序的标题
st.set_page_config(page_title="app name", layout="wide")
# Helper function to generate random ASCII strings for demonstration purposes
def random_string(length):
return ''.join(chr(random.randint(33, 126)) for _ in range(length))
menu1="菜单1"
menu2="菜单2"
# Use inscode.ai to generate a random prompt if not provided
def generate_prompt(prompt):
if not prompt:
prompt = "Please provide a prompt for the AI to generate a random one for you."
st.warning(prompt)
prompt = inscode.ai("Generate a random prompt", random_string(20))
return prompt
with st.sidebar:
menu = option_menu("菜单", [menu1, menu2],
icons=['house', "list-task"],
menu_icon="cast", default_index=0)
# Define Streamlit app
def app():
st.set_page_config(page_title="Lottery App")
st.title("Lottery App")
# Initialize variables
num_awards = 0
awards = {}
winners = []
def main():
# Define function to add an award
def add_award(award, prize):
nonlocal num_awards
awards[award] = prize
num_awards += 1
if menu == menu1:
st.subheader(f"{menu1}")
# Define function to remove an award
def remove_award(award):
nonlocal num_awards
if award in awards:
del awards[award]
num_awards -= 1
if menu == menu2:
st.subheader(f"{menu2}")
# Define function to clear all awards
def clear_awards():
nonlocal num_awards, awards
num_awards = 0
awards = {}
# Define function to draw a winner
def draw_winner():
if num_awards < 1:
st.warning("Please add at least one award before drawing a winner.")
return
winner = random.choice([x for x in range(num_awards) if x not in winners])
winners.append(winner)
award = list(awards.keys())[winner]
prize = awards[award]
st.success(f"Congratulations! You won {prize} - {award}")
# Define function to display the awards
def display_awards():
st.header("Awards")
if num_awards < 1:
st.warning("No awards added. Click 'Add Award' to add an award.")
else:
for i, award in enumerate(awards.keys()):
st.write(f"{i+1}. {award} - {awards[award]}")
# Define function to display the winners
def display_winners():
st.header("Winners")
if len(winners) < 1:
st.warning("No winners drawn. Click 'Draw Winner' to draw a winner.")
else:
for i, winner in enumerate(winners):
award = list(awards.keys())[winner]
prize = awards[award]
st.write(f"{i+1}. {award} - {prize}")
# Define function to clear all winners
def clear_winners():
nonlocal winners
winners = []
# Define sidebar
with st.sidebar:
st.header("Options")
add = st.button("Add Award")
remove = st.button("Remove Award")
clear = st.button("Clear Awards")
st.write(" ")
draw = st.button("Draw Winner")
clear_win = st.button("Clear Winners")
st.write(" ")
prompt = st.text_input("Enter prompt for AI to generate random prompt (optional):")
generate = st.button("Generate Random Prompt")
# Process sidebar input
if add:
award = st.text_input("Award:")
prize = st.text_input("Prize:")
if award and prize:
add_award(award, prize)
if remove:
if num_awards < 1:
st.warning("No awards to remove!")
else:
award = st.selectbox("Select award to remove:", list(awards.keys()))
remove_award(award)
if clear:
clear_awards()
if draw:
draw_winner()
if clear_win:
clear_winners()
if generate:
prompt = generate_prompt(prompt)
# Display awards and winners
display_awards()
display_winners()
if __name__ == '__main__':
main()
app()
\ No newline at end of file
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册