No /GET endpoint when using flask-restx with Python

Using RStudio Connect to deploy a flask API from python (rsconnect library). I really like the idea of adding swagger docs so using the flask-restx package, with guiding examples from RStudio docs (https://github.com/sol-eng/python-examples/blob/master/flask-restx/predict.py and https://docs.rstudio.com/connect/user/flask/#flask-restx).

Following this though, I'm getting this error upon deployment using rsconnect: " You are seeing this page because the Python API at this location does not provide a resource at the root ( GET / )". Has anyone ever encountered this before? If so, any troubleshooting advice?

I'm including my code below for added clarity:


## file is named: app.py

model = hub.load(model_folder)

def  make_prediction(*, input_data) -> dict:
    """Make a prediction using saved tf model.
    Args:
        input_data: Array of model prediction inputs.
    Returns:
        Predictions for each input row
    """

    data = tf.convert_to_tensor([input_data])
    
    ## loaded above
    prediction = model(data).numpy()

    return prediction.tolist()

app = Flask(__name__)

## added this because I kept getting CORS error-- hiding the actual server route here though
## this deviates from the RStudio example so maybe here?

app.config["SERVER_NAME"] = "base_url:port/content/#/"

# define the flask-restx boilerplate

api = Api(
    app, 
    version="1.0.0", 
    title="Model API", 
    description="Predicts something"
)

# define the main subroute for requests

ns = api.namespace("predict", description="Predict something")

# define the API response

model_predict = api.model(
    "Prediction",
    {
        "input": fields.String(required=True, description="text"),
        "output": fields.Float(description = "Predicted")
    },
)

# GET example that accepts a new data point as a query parameter in the URL path
# served at route + namespace, so at /predict/<user's text input>

@ns.route("/<string:text>")
@ns.param("input", "new text")
class Predict(Resource):
    @ns.marshal_with(model_predict)
    @ns.doc("get text ")
    def get(self, input):
        prediction = make_prediction(input_data=input)
        return jsonify({'output': prediction,  'input': input})

Hi @markpreston,
As you suspected -- This line where you're trying to set base_url as your hostname is creating an issue:

We'll be adding CORS support in the next release of Connect which should enable you to remove that app.config line. In the meantime you may need to use a proxy to get around CORS, but I'll have your CS rep reach out with more information on when to expect our next release.

Okay glad to have that feedback. How does proxying work? Do you have any suggested routes to get around the issue aside from that? Avoiding the flask-restx package until the update? Any insight would be much appreciated. Thanks for the starting point though :slight_smile:

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

If you have a query related to it or one of the replies, start a new topic and refer back with a link.