如何在 streamlit 程序中保留跨应用程序模式的更改?
How to retain changes across app modes in a streamlit program?
代码如下:
def main():
st.title("Image Restoration")
#define states of the app
colorizationPage = "Image Colorization"
inpaintingPage = "Image Inpainting"
enhancementPage = "Image Enhacement"
denoisingPage = "Image Denoising"
appMode = st.sidebar.selectbox(
"Choose the app mode",
[
colorizationPage,
inpaintingPage,
enhancementPage,
denoisingPage
])
st.header(appMode)
# prompt for uploading an image
imgFile = st.file_uploader("Choose an image file")
if imgFile is None:
st.text("You haven't uploaded an image file")
else:
image = Image.open(imgFile)
# run the python script corresponding to appMode
# image colorization
if appMode == colorizationPage:
original, colorized = st.columns(2)
# get the colorized image
result = colorize.colorize(image)
#display the images side by side
original.header("Original")
original.image(image, use_column_width = True)
colorized.header("Colorized")
colorized.image(result, use_column_width = True)
# image enhancement
if appMode == enhancementPage:
original, enhanced = st.columns(2)
# get the enhanced image
res = night_images.enhance(image)
# display the images
original.header("Original Image")
original.image(image, use_column_width = True)
enhanced.header("Denoised Image")
enhanced.image(res, use_column_width = True)
更改应用程序模式后,即使我将结果重新分配给图像本身,对图像所做的更改也会反转。我将如何保留所做的更改?
我尝试更改上传图片的提示以检查是否已上传图片以不再重新上传,但这似乎也不起作用。
您应该使用会话状态来保存此类信息 -
https://docs.streamlit.io/library/api-reference/session-state
您可以将其视为在页面重新加载时不会丢失的字典。
对于你的情况,可以这样写
appMode = st.sidebar.selectbox(
"Choose the app mode",
[
colorizationPage,
inpaintingPage,
enhancementPage,
denoisingPage
])
# New code
st.session_state['app_mode'] = appMode
# End
应该可以解决您的问题。
每次加载这个我也会将信息保存在列表中并获取索引。
app_modes = [
colorizationPage,
inpaintingPage,
enhancementPage,
denoisingPage
]
selected_app_mode = st.session_state.get('app_mode')
app_mode_ix = 0
if selected_app_mode:
app_mode_ix = app_modes.index(selected_app_mode)
appMode = st.sidebar.selectbox(app_modes, index=app_mode_ix)
st.session_state['app_mode'] = appMode
代码如下:
def main():
st.title("Image Restoration")
#define states of the app
colorizationPage = "Image Colorization"
inpaintingPage = "Image Inpainting"
enhancementPage = "Image Enhacement"
denoisingPage = "Image Denoising"
appMode = st.sidebar.selectbox(
"Choose the app mode",
[
colorizationPage,
inpaintingPage,
enhancementPage,
denoisingPage
])
st.header(appMode)
# prompt for uploading an image
imgFile = st.file_uploader("Choose an image file")
if imgFile is None:
st.text("You haven't uploaded an image file")
else:
image = Image.open(imgFile)
# run the python script corresponding to appMode
# image colorization
if appMode == colorizationPage:
original, colorized = st.columns(2)
# get the colorized image
result = colorize.colorize(image)
#display the images side by side
original.header("Original")
original.image(image, use_column_width = True)
colorized.header("Colorized")
colorized.image(result, use_column_width = True)
# image enhancement
if appMode == enhancementPage:
original, enhanced = st.columns(2)
# get the enhanced image
res = night_images.enhance(image)
# display the images
original.header("Original Image")
original.image(image, use_column_width = True)
enhanced.header("Denoised Image")
enhanced.image(res, use_column_width = True)
更改应用程序模式后,即使我将结果重新分配给图像本身,对图像所做的更改也会反转。我将如何保留所做的更改?
我尝试更改上传图片的提示以检查是否已上传图片以不再重新上传,但这似乎也不起作用。
您应该使用会话状态来保存此类信息 - https://docs.streamlit.io/library/api-reference/session-state
您可以将其视为在页面重新加载时不会丢失的字典。
对于你的情况,可以这样写
appMode = st.sidebar.selectbox(
"Choose the app mode",
[
colorizationPage,
inpaintingPage,
enhancementPage,
denoisingPage
])
# New code
st.session_state['app_mode'] = appMode
# End
应该可以解决您的问题。
每次加载这个我也会将信息保存在列表中并获取索引。
app_modes = [
colorizationPage,
inpaintingPage,
enhancementPage,
denoisingPage
]
selected_app_mode = st.session_state.get('app_mode')
app_mode_ix = 0
if selected_app_mode:
app_mode_ix = app_modes.index(selected_app_mode)
appMode = st.sidebar.selectbox(app_modes, index=app_mode_ix)
st.session_state['app_mode'] = appMode