I’m trying to set GOOGLE_APPLICATION_CREDENTIALS in Streamlit, to use Google APIs.
I’m doing so via the via the file uploader - please find the code below:
uploaded_file = st.file_uploader("File 1", type="json")
import os
if uploaded_file is not None:
data = pd.read_json(uploaded_file, orient='index')
os.environ['GOOGLE_APPLICATION_CREDENTIALS'] = data
st.write(data)
Here’s the error I get:
TypeError: str expected, not DataFrame
So if I understand correctly, I need to get a string (that is, the path to the JSON file), whereas read_json outputs a dataframe.
Apparently the environment expects a path to a json file. So what you need to do after uploading your file is to save it in a temporary json file and specify its path with os.environ.
I wrote this with the tempfile lib based on one of your previous posts on the Streamlit forum:
import tempfile
uploaded_file = st.file_uploader("File 1", type="json")
if uploaded_file is not None:
with tempfile.NamedTemporaryFile(mode='w') as fp:
fp.write(uploaded_file.getvalue())
path = fp.name
st.write(path)
os.environ['GOOGLE_APPLICATION_CREDENTIALS'] = path
Turns out my temp file is indeed created (yay!), but not found - as per the screenshot below:
Beware that your temporary file will exist only during the execution of the with block. Thus you must be sure that the part which processes the environment variable and read the json file lives inside that block.
Thanks Synode - I didn’t realize they had to be in the same block, good to know!
So hopefully we’re moving forward, as when I indent all functions/parts processing the environment variable within the same block, the temp file seems to be found!
The new issue is that there’s now a PermissionError, as follows:
As a workaround, add the delete=False parameter to NamedTemporaryFile, save the json inside the with block, then put back your processing code outside it. However you have to manually delete your temporary file once your app finishes to run.
I suggest you wrap your processing code in a try/finally block to make sure the file will be deleted, even in case of exception.
import tempfile
uploaded_file = st.file_uploader("File 1", type="json")
with tempfile.NamedTemporaryFile(mode='w', delete=False) as fp:
fp.write(uploaded_file.getvalue())
try:
os.environ['GOOGLE_APPLICATION_CREDENTIALS'] = fp.name
# Code using GOOGLE_APPLICATION_CREDENTIALS
finally:
os.unlink(fp.name)
So you’ve figured out what the problem was? I didn’t have enough time to test the code you’ve shared yesterday, but my first guess would be that your app runs after my try/finally block, which would explain why the file wasn’t found.
with open(fp.name) as a:
st.write(a.read())
#client = language.LanguageServiceClient()
client = language_v1.LanguageServiceClient()
You need to open the file for your code to work? If you remove that with open() line and “outdent” the code inside that block, you’re encoutering your previous issue again?
Initially the with open() was only to demonstrate that I could re-open the file and read its content. It wasn’t intended to be part of the solution
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.