Failed to establish a new connection: [Errno 61] Connection refused')

Hello Everyone,
Streamlit app failed to connect Flask api. And I’m struggling finding way to solve the issue. Here is Streamlit side:

st.set_page_config(page_title="Visualisation état capteurs ", layout='wide')
st.title('Maintenance prédictive')
st.info('remontée mesures capteurs')

# Set connection
session = requests.Session()

# Définition base url
base_url = 'http://localhost:5000'

# Construction requête récupération des ID équipements.
end_point_id = '/id'
url_id = base_url + end_point_id
print(url_id)


def get_id(session, url_id):
    try:
        headers = {
            'User-Agent': 'Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/86.0.4240.111 Safari/537.36'
        }
        results_id = session.get(url_id, headers = headers)
        retry = Retry(connect=3, backoff_factor=1)
        adapter = HTTPAdapter(max_retries=retry)
        session.mount('http://', adapter)
        session.mount('https://', adapter)

        results_id = results_id.json()
        return results_id
    except Exception as e:
        print("An exception occurred:", e)
        traceback.print_exc()
        return {}

liste_id = get_id(session, url_id)

Here is the Flask side:

@app.route('/id', methods=['GET'])
def liste_id():
       df_id = df['ID'].tolist()
       unique_ids = np.unique(df_id)
       unique_ids = unique_ids.tolist()
       return unique_ids

Error message:
``raise ConnectionError(e, request=request)
requests.exceptions.ConnectionError: HTTPConnectionPool(host=‘localhost’, port=5000): Max retries exceeded with url: /id (Caused by NewConnectionError(‘<urllib3.connection.HTTPConnection object at 0x1753694b0>: Failed to establish a new connection: [Errno 61] Connection refused’))```

Flask api works (checked)
Here is pip list:

------------------------- ------------
altair                    5.1.2
attrs                     23.1.0
blinker                   1.6.3
cachetools                5.3.1
certifi                   2023.7.22
charset-normalizer        3.3.0
click                     8.1.7
contourpy                 1.1.1
cycler                    0.11.0
Flask                     3.0.0
fonttools                 4.42.1
gitdb                     4.0.10
GitPython                 3.1.37
idna                      3.4
imbalanced-learn          0.11.0
importlib-metadata        6.8.0
itsdangerous              2.1.2
Jinja2                    3.1.2
joblib                    1.3.2
jsonschema                4.19.1
jsonschema-specifications 2023.7.1
kiwisolver                1.4.5
markdown-it-py            3.0.0
MarkupSafe                2.1.3
matplotlib                3.8.0
mdurl                     0.1.2
numpy                     1.26.0
packaging                 23.1
pandas                    2.1.1
Pillow                    10.0.1
pip                       23.3
protobuf                  4.24.4
pyarrow                   13.0.0
pydeck                    0.8.1b0
Pygments                  2.16.1
pyparsing                 3.1.1
python-dateutil           2.8.2
pytz                      2023.3.post1
referencing               0.30.2
requests                  2.31.0
rich                      13.6.0
rpds-py                   0.10.6
scikit-learn              1.3.1
scipy                     1.11.2
seaborn                   0.12.2
setuptools                65.5.0
six                       1.16.0
smmap                     5.0.1
streamlit                 1.27.2
tenacity                  8.2.3
threadpoolctl             3.2.0
toml                      0.10.2
toolz                     0.12.0
tornado                   6.3.3
typing_extensions         4.8.0
tzdata                    2023.3
tzlocal                   5.1
urllib3                   2.0.6
validators                0.22.0
Werkzeug                  3.0.0
wheel                     0.41.2
xgboost                   2.0.0
zipp                      3.17.0

I need your help to understand why it fails to connect. Many thanks in advance.

Hello All, I finally worked out the issue with adding jsonify() as ending line of code in api: @app.route('/id', methods=['GET']) def liste_id(): ids = df.loc[:, 'ID'].values.tolist() unique_ids = np.unique(ids) unique_ids = unique_ids.tolist() print(jsonify(unique_ids)) return jsonify(unique_ids)
This way, it is understandable by Streamlit app.
I hope it will help :smiley:

This topic was automatically closed 2 days after the last reply. New replies are no longer allowed.