How can I make the program reload JSON data when refreshing the browser? #2783
-
QuestionThe JSON file is a list that contains a dictionary. This is the example code: def get_ana(file):
if not os.path.exists(file):
ana_messages = file_not_found_error(file)
ana_status = False
return ana_status, ana_messages
else:
# asyncio.run(load_ana(file))
with open(file, 'r') as f:
ana_messages = json.load(f)
number = random.randint(0, len(ana_messages) - 1)
print(number)
ana_messages = ana_messages[number]
ana_status = True
return ana_status, ana_messages
ana = get_ana("data/a.json")
if not ana[0]:
id = ana[1][0]["id"]
msg = ana[1][0]["msg"]
author = ana[1][0]["author"]
time = ana[1][0]["time"]
else:
id = ana[1]["id"]
msg = ana[1]["msg"]
author = ana[1]["author"]
time = ana[1]["time"]
with ui.card().classes("absolute-center"):
with ui.row():
ui.badge("id")
ui.label(id)
with ui.row():
ui.badge("语录")
ui.label(msg)
with ui.row():
ui.badge("作者")
ui.label(author)
with ui.row():
ui.badge("时间")
ui.label(time) |
Beta Was this translation helpful? Give feedback.
Answered by
falkoschindler
Mar 29, 2024
Replies: 1 comment 2 replies
-
Hi @SHDocter, You can move your code into a page function, which is called every time a page is requested: @ui.page('/')
def main_page():
... |
Beta Was this translation helpful? Give feedback.
2 replies
Answer selected by
SHDocter
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Hi @SHDocter,
You can move your code into a page function, which is called every time a page is requested: