Skip to main content
cancel
Showing results for 
Search instead for 
Did you mean: 

Join us at FabCon Vienna from September 15-18, 2025, for the ultimate Fabric, Power BI, SQL, and AI community-led learning event. Save €200 with code FABCOMM. Get registered

Reply
AmanKumar
New Member

api connection issues from local to fabric notebook

When I am using my local flask api in fabric notebook I am getting this error.

from flask import Flask, request, jsonify
import openai
from openai import AzureOpenAI

# Configuration for Azure OpenAI API

# Initialize Flask app
app = Flask(__name__)

# Initialize the Azure OpenAI client
client = AzureOpenAI(
    azure_endpoint=ENDPOINT_URL,
    api_key=API_KEY,
    api_version=API_VERSION,
)

# System message for OpenAI GPT
MESSAGES = [
    {"role": "system", "content": """you are a food concierge. your job is to tell me how long a product will last in my fridge or pantry. I will tell you the name of an edible product,
you will simply respond to the amount of days that I can expect the product to be good to eat for from the date of purchase.
for example if I ask "eggs" you would respond with "fridge: 20-30 days".
Always respond in amount of days."""}
]

@app.route('/get_expiry', methods=['POST'])
def get_expiry():
    # Retrieve product name from POST request JSON
    data = request.get_json()
    if not data or 'product' not in data:
        return jsonify({"error": "Please provide a product name"}), 400

    product = data['product']
   
    # Add product to the messages for OpenAI model
    MESSAGES.append({"role": "user", "content": product})
   
    try:
        # Call the Azure OpenAI API to get the expiry information
        completion = client.chat.completions.create(
            model=MODEL_NAME,
            messages=MESSAGES,
            temperature=0.2
        )
       
        # Extract and return the result
        expirytext = completion.choices[0].message.content
        return jsonify({"product": product, "expiry": expirytext}), 200
   
    except Exception as e:
        return jsonify({"error": str(e)}), 500

# Run the Flask app
if __name__ == '__main__':
    app.run(debug=True)

 

import requests
import json

# Define the API endpoint

# Create the payload with the product name
data = {
    'product': 'Milk'  # Replace with the actual product you want to inquire about
}

# Send the POST request with JSON data
response = requests.post(url, json=data)

# Check the response
if response.status_code == 200:
    result = response.json()
    print(f"Product: {result['product']}")
    print(f"Expiry Information: {result['expiry']}")
else:
    print(f"Error: {response.json()['error']}")
 

ConnectionError: HTTPSConnectionPool(host='127.0.0.1', port=5000): Max retries exceeded with url: /get_expiry (Caused by NewConnectionError('<urllib3.connection.HTTPSConnection object at 0x7bbf00902110>: Failed to establish a new connection: [Errno 111] Connection refused'))

1 ACCEPTED SOLUTION
nilendraFabric
Community Champion
Community Champion

Hi @AmanKumar 

 

A local Flask application cannot be run directly in Microsoft Fabric notebooks.

This is because Fabric notebooks operate within an isolated network environment, which prevents direct access to local resources or servers like your Flask API.

 

Microsoft Fabric has security measures in place that prevent connecting to external local services within its environment

 

Try these:

 

Adapt Your Code: Rewrite your Flask app logic to work within Fabric’s notebook environment using PySpark and other supported libraries.
2. External API Deployment: Deploy your Flask app to a cloud service that provides a public URL, then make API calls to it from your Fabric notebook.

 

please give kudos and accept this solution if this is helpful.

 

thanks

Nilendra

 

View solution in original post

5 REPLIES 5
v-menakakota
Community Support
Community Support

Hi @AmanKumar ,

May I ask if you have resolved this issue? If so, please mark the helpful reply and accept it as the solution. This will be helpful for other community members who have similar problems to solve it faster.

Thank you.

 

Hi @AmanKumar ,

I hope this information is helpful. Please let me know if you have any further questions or if you'd like to discuss this further. If this answers your question, please Accept it as a solution and give it a 'Kudos' so others can find it easily.

Thank you.

Hi @AmanKumar ,

I wanted to check if you had the opportunity to review the information provided. Please feel free to contact us if you have any further questions. If my response has addressed your query, please accept it as a solution and give a 'Kudos' so other members can easily find it.

Regards,
Menaka.

v-menakakota
Community Support
Community Support

Hi @AmanKumar ,

Thank you for reaching out to us on the Microsoft Fabric Community Forum.

As @nilendraFabric  mentioned,a local Flask application cannot be directly accessed from a Microsoft Fabric notebook.

nilendraFabric
Community Champion
Community Champion

Hi @AmanKumar 

 

A local Flask application cannot be run directly in Microsoft Fabric notebooks.

This is because Fabric notebooks operate within an isolated network environment, which prevents direct access to local resources or servers like your Flask API.

 

Microsoft Fabric has security measures in place that prevent connecting to external local services within its environment

 

Try these:

 

Adapt Your Code: Rewrite your Flask app logic to work within Fabric’s notebook environment using PySpark and other supported libraries.
2. External API Deployment: Deploy your Flask app to a cloud service that provides a public URL, then make API calls to it from your Fabric notebook.

 

please give kudos and accept this solution if this is helpful.

 

thanks

Nilendra

 

Helpful resources

Announcements
Join our Fabric User Panel

Join our Fabric User Panel

This is your chance to engage directly with the engineering team behind Fabric and Power BI. Share your experiences and shape the future.

June FBC25 Carousel

Fabric Monthly Update - June 2025

Check out the June 2025 Fabric update to learn about new features.

June 2025 community update carousel

Fabric Community Update - June 2025

Find out what's new and trending in the Fabric community.