Unable to host streamlit app using heroku due to error: cannot create /app/.streamlit/config.toml

GitHub link: GitHub - siddheshshankar/Covid-availability-check

It contains 4 files:
1.setup.sh
2. Procfile
3. requirements.txt
4. app.py

setup.sh

mkdir -p ~/.streamlit

echo "[server]
headless = true
port = $PORT
enableCORS = false
" > ~/.streamlit/config.toml

Procfile

web: sh setup.sh && streamlit run app.py

requirements.txt

streamlit==0.81.0
pandas==1.2.3
requests==2.25.1
numpy==1.20.1

Error log

2021-05-04T04:19:00.000000+00:00 app[api]: Build started by user xyz@gmail.com
2021-05-04T04:19:09.402102+00:00 heroku[web.1]: State changed from crashed to starting
2021-05-04T04:19:17.645929+00:00 heroku[web.1]: Starting process with command `sh setup.sh && streamlit run app.py`
2021-05-04T04:19:19.503695+00:00 app[web.1]: setup.sh: 2:
2021-05-04T04:19:19.503719+00:00 app[web.1]: setup.sh: 3: cannot create /app/.streamlit/config.toml
2021-05-04T04:19:19.539427+00:00 heroku[web.1]: Process exited with status 2
2021-05-04T04:19:19.621673+00:00 heroku[web.1]: State changed from starting to crashed
2021-05-04T04:19:46.775917+00:00 app[api]: Deploy caee1d3e by user xyz@gmail.com
2021-05-04T04:19:46.775917+00:00 app[api]: Release v6 created by user siddheshshanker@gmail.com
2021-05-04T04:19:46.941267+00:00 heroku[web.1]: State changed from crashed to starting
2021-05-04T04:19:55.876740+00:00 heroku[web.1]: Starting process with command `sh setup.sh && streamlit run app.py`
2021-05-04T04:19:57.738434+00:00 app[web.1]: setup.sh: 2:
2021-05-04T04:19:57.738471+00:00 app[web.1]: setup.sh: 3: cannot create /app/.streamlit/config.toml
2021-05-04T04:19:57.798153+00:00 heroku[web.1]: Process exited with status 2
2021-05-04T04:19:57.867653+00:00 heroku[web.1]: State changed from starting to crashed
2021-05-04T04:20:09.000000+00:00 app[api]: Build succeeded
2021-05-04T04:21:55.995211+00:00 heroku[router]: at=error code=H10 desc="App crashed" method=GET path="/" host=cowin-india.herokuapp.com request_id=1cd1e24e-4248-4905-afb5-e545fd685f97 fwd="182.68.54.180" dyno= connect= service= status=503 bytes= protocol=https
2021-05-04T04:21:56.547345+00:00 heroku[router]: at=error code=H10 desc="App crashed" method=GET path="/favicon.ico" host=cowin-india.herokuapp.com request_id=525bcab0-f490-4322-a32a-d222a35903fb fwd="182.68.54.180" dyno= connect= service= status=503 bytes= protocol=https

Can anyone help me debug and fix this issue?

Hi @Siddhesh_Shankar, welcome to the Streamlit community!! :partying_face: :tada:

It might help to go over some solutions that helped people with the same error. I’d recommend going over this thread. Unrolling setup.sh has worked for at least one user:

mkdir -p ~/.streamlit/
echo "[server]"  > ~/.streamlit/config.toml 
echo "headless = true"  >> ~/.streamlit/config.toml
echo "port = $PORT"  >> ~/.streamlit/config.toml
echo "enableCORS = false"  >> ~/.streamlit/config.toml

Let us know if you still need help after implementing solutions from Not able to deploy to Heroku | Error in creating config.toml, Deploying Heroku Error, and disabling CORS.

I would defer to our awesome community members including @andfanilo and @asehmi who know more about Streamlit <> Heroku

Happy Streamlit-ing! :balloon:
Snehan

Hello @snehankekre/@andfanilo, Hope you are doing well!
I just copied the code snippet into setup.sh and the config.toml issue got resolved. However, still my application is throwing some other error which I am unable to debug.
Below are the logs:

2021-05-04T06:52:58.975780+00:00 app[api]: Release v5 created by user siddheshshanker@gmail.com
2021-05-04T06:52:59.151376+00:00 heroku[web.1]: State changed from crashed to starting
2021-05-04T06:53:11.431610+00:00 heroku[web.1]: Starting process with command `sh setup.sh && streamlit run app.py`
2021-05-04T06:53:15.908135+00:00 app[web.1]: 2021-05-04 06:53:15.907
2021-05-04T06:53:15.908190+00:00 app[web.1]: Warning: the config option 'server.enableCORS=false' is not compatible with 'server.enableXsrfProtection=true'.
2021-05-04T06:53:15.908191+00:00 app[web.1]: As a result, 'server.enableCORS' is being overridden to 'true'.
2021-05-04T06:53:15.908191+00:00 app[web.1]:
2021-05-04T06:53:15.908191+00:00 app[web.1]: More information:
2021-05-04T06:53:15.908196+00:00 app[web.1]: In order to protect against CSRF attacks, we send a cookie with each request.
2021-05-04T06:53:15.908197+00:00 app[web.1]: To do so, we must specify allowable origins, which places a restriction on
2021-05-04T06:53:15.908197+00:00 app[web.1]: cross-origin resource sharing.
2021-05-04T06:53:15.908197+00:00 app[web.1]:
2021-05-04T06:53:15.908201+00:00 app[web.1]: If cross origin resource sharing is required, please disable server.enableXsrfProtection.
2021-05-04T06:53:15.908202+00:00 app[web.1]:
2021-05-04T06:53:21.000000+00:00 app[api]: Build succeeded
2021-05-04T06:53:22.665439+00:00 app[web.1]: 2021-05-04 06:53:22.665
2021-05-04T06:53:22.665460+00:00 app[web.1]: Warning: the config option 'server.enableCORS=false' is not compatible with 'server.enableXsrfProtection=true'.
2021-05-04T06:53:22.665462+00:00 app[web.1]: As a result, 'server.enableCORS' is being overridden to 'true'.
2021-05-04T06:53:22.665463+00:00 app[web.1]:
2021-05-04T06:53:22.665463+00:00 app[web.1]: More information:
2021-05-04T06:53:22.665464+00:00 app[web.1]: In order to protect against CSRF attacks, we send a cookie with each request.
2021-05-04T06:53:22.665465+00:00 app[web.1]: To do so, we must specify allowable origins, which places a restriction on
2021-05-04T06:53:22.665465+00:00 app[web.1]: cross-origin resource sharing.
2021-05-04T06:53:22.665465+00:00 app[web.1]:
2021-05-04T06:53:22.665466+00:00 app[web.1]: If cross origin resource sharing is required, please disable server.enableXsrfProtection.
2021-05-04T06:53:22.665466+00:00 app[web.1]:
2021-05-04T06:53:23.620175+00:00 app[web.1]:
2021-05-04T06:53:23.620220+00:00 app[web.1]: You can now view your Streamlit app in your browser.
2021-05-04T06:53:23.620262+00:00 app[web.1]:
2021-05-04T06:53:23.620561+00:00 app[web.1]: Network URL: http://172.17.26.222:8501
2021-05-04T06:53:23.620574+00:00 app[web.1]: External URL: http://35.175.141.121:8501
2021-05-04T06:53:23.620575+00:00 app[web.1]:
2021-05-04T06:54:11.674442+00:00 heroku[web.1]: Error R10 (Boot timeout) -> Web process failed to bind to $PORT within 60 seconds of launch
2021-05-04T06:54:11.722121+00:00 heroku[web.1]: Stopping process with SIGKILL
2021-05-04T06:54:11.846436+00:00 heroku[web.1]: Process exited with status 137
2021-05-04T06:54:11.918574+00:00 heroku[web.1]: State changed from starting to crashed
2021-05-04T06:54:13.413464+00:00 heroku[router]: at=error code=H10 desc="App crashed" method=GET path="/" host=cowin-india.herokuapp.com request_id=1f16fed1-9853-4ca2-a627-928b8a819c06 fwd="49.37.167.250" dyno= connect= service= status=503 bytes= protocol=https
2021-05-04T06:54:15.098767+00:00 heroku[router]: at=error code=H10 desc="App crashed" method=GET path="/" host=cowin-india.herokuapp.com request_id=423484e5-f100-4687-bd2d-9ff1763faa81 fwd="49.37.167.250" dyno= connect= service= status=503 bytes= protocol=https
2021-05-04T06:54:21.200823+00:00 heroku[router]: at=error code=H10 desc="App crashed" method=GET path="/" host=cowin-india.herokuapp.com request_id=4416cf95-c793-46c3-9a95-ed2efdf1968e fwd="49.37.167.250" dyno= connect= service= status=503 bytes= protocol=https
2021-05-04T06:54:23.779227+00:00 heroku[router]: at=error code=H10 desc="App crashed" method=GET path="/" host=cowin-india.herokuapp.com request_id=17a982e8-82f9-490f-a8f0-982b3f36ae43 fwd="49.37.167.250" dyno= connect= service= status=503 bytes= protocol=https
2021-05-04T06:55:04.744458+00:00 heroku[router]: at=error code=H10 desc="App crashed" method=GET path="/" host=cowin-india.herokuapp.com request_id=bfc0c905-0ee4-4756-a67a-86458d25e968 fwd="182.68.54.180" dyno= connect= service= status=503 bytes= protocol=https
2021-05-04T06:55:05.321278+00:00 heroku[router]: at=error code=H10 desc="App crashed" method=GET path="/favicon.ico" host=cowin-india.herokuapp.com request_id=b5c980ef-d36c-49b0-b92a-d381fb8c6afb fwd="182.68.54.180" dyno= connect= service= status=503 bytes= protocol=https

Could you please help me out with this?

@Siddhesh_Shankar Congrats on the solving the problem! :tada: :tada: :partying_face:

I see what worked was passing the $PORT environment variable to your Streamlit app within your Procfile:

web: sh setup.sh && streamlit run --server.port $PORT app.py

Fantastic! I also really appreciate that your app is pulling from the cowin CDN rather than putting pressure on the main API. Good decision!

Best, :balloon:
Snehan

Yes thanks a lot @snehankekre. App is working fine now.

1 Like

@snehankekre
My app was running fine. Suddenly I started encountering this error message:

File "/app/.heroku/python/lib/python3.9/site-packages/streamlit/script_runner.py", line 337, in _run_script
    exec(code, module.__dict__)
File "/app/app.py", line 111, in <module>
    read_json = response.json()
File "/app/.heroku/python/lib/python3.9/site-packages/requests/models.py", line 900, in json
    return complexjson.loads(self.text, **kwargs)
File "/app/.heroku/python/lib/python3.9/json/__init__.py", line 346, in loads
    return _default_decoder.decode(s)
File "/app/.heroku/python/lib/python3.9/json/decoder.py", line 337, in decode
    obj, end = self.raw_decode(s, idx=_w(s, 0).end())
File "/app/.heroku/python/lib/python3.9/json/decoder.py", line 355, in raw_decode
    raise JSONDecodeError("Expecting value", s, err.value) from None

Could you please guide me how to go about it? This app is working fine on my local machine.

Hi @Siddhesh_Shankar,

Based on the error message, it does not look like a Streamlit-specific issue. My best guess is that you need to figure out how to parse json responses from the API you’re querying and handle exceptions.

It might be the case that the cowin API is either rate-limiting or banning your heroku IP and returning a malformed response that isn’t being handled in lines 110-111

Happy Streamlit-ing! :balloon:
Snehan

1 Like

Thanks, But it is working fine locally which means there is nothing wrong with the code. Seems to be issue in heroku/streamlit

I don’t think it’s a good idea to put the bulk of your code within the st.button scope in app.py, line 23. Try replacing that with st.checkbox which will run the code when checked, and keep it in scope until unchecked. It might be that the code hasn’t fully run at the time the button is deactivated and therefore throws an error. At best it’ll be intermittently correct.

2 Likes

@asehmi Tried. Getting the same error. I am struggling to understand that how is it working absolutely fine in my local machine and why am I getting a JSON decode error when deployed on heroku. Initially, it was working fine.

This may be a CORS issue. Can you print the body of your web response to see what’s in the payload?

@asehmi
I am getting response 403. How do I fix this?

I suspect it isn’t a CORS issue. I think I was on the right track in my previous response. @Siddhesh_Shankar’s heroku IP is blocked/banned by cowin’s CDN. All non-Indian IP addresses get blocked in time.

This is what I see when I try to access the API using various VPN providers:

Is there any way out? How to fix this?
Trying this:

    ua = UserAgent()
    header = {'User-Agent':str(ua.chrome)}
    data_list = []
    for date in date_range:
        response = requests.get(f"https://cdn-api.co-vin.in/api/v2/appointment/sessions/public/calendarByPin?pincode={pincodes}&date={date}", headers=header)
        read_json = response.json()

Sounds like you need to issue the request through a proxy based in India.

1 Like