Not sure if there’d be a pretty way to change a function’s parameters via st.dropdown if these parameters are actually located in a secondary Python file? Please see screenshot below for context:
So what you want to do is change the behavior of that suggests library, and more specifically add a custom parameter to change the language part of the following hardcoded string:
The cleanest solution would be to fork and patch the library to add a new parameter, create your PyPI and build your streamlit app on top of it.
Another solution would be to patch the library’s function directly from your app.
import streamlit as st
from suggests import suggests
def custom_get_google_url():
# Retrieve language from session state, or set lang to 'en' by default
lang = st.session_state.get("google_url_language", "en")
return f"https://www.google.com/complete/search?sclient=psy-ab&hl={lang}&q="
# Select your language, and store it in session state as 'google_url_language'
st.selectbox("Language", ["en", "fr", "es"], key="google_url_language")
# Here we replace suggests's function by our own
suggests.get_google_url = custom_get_google_url
Two things to note here:
You must do an import suggests instead of from suggests import get_google_url to patch the function, like done above. suggests.get_google_url = ... redefines the function used by the library, whereas get_google_url = ... is just a regular assignment.
Patching a library’s function is done globally, so to make sure each user session uses its own language parameter, I retrieve the latter through a very basic session state implementation which will keep track of the language selected by each user. Without session state, you might run into race condition issues where one user might use the language parameter of another user connected to your app.
Thanks for stopping by! We use cookies to help us understand how you interact with our website.
By clicking “Accept all”, you consent to our use of cookies. For more information, please see our privacy policy.
Cookie settings
Strictly necessary cookies
These cookies are necessary for the website to function and cannot be switched off. They are usually only set in response to actions made by you which amount to a request for services, such as setting your privacy preferences, logging in or filling in forms.
Performance cookies
These cookies allow us to count visits and traffic sources so we can measure and improve the performance of our site. They help us understand how visitors move around the site and which pages are most frequently visited.
Functional cookies
These cookies are used to record your choices and settings, maintain your preferences over time and recognize you when you return to our website. These cookies help us to personalize our content for you and remember your preferences.
Targeting cookies
These cookies may be deployed to our site by our advertising partners to build a profile of your interest and provide you with content that is relevant to you, including showing you relevant ads on other websites.