Flask App Route + Hops

Hello,

I am having some issue getting the routes to work for a Flask app, while using Hops. Here is the sample code:

from flask import Flask, request, render_template
import ghhops_server as hs


#Register hops as middleware
app = Flask(__name__)
app.debug = True
hops: hs.HopsFlask = hs.Hops(app)

@app.route("/help")
def help():
    return "Welcome to Grasshopper Hops for CPython!"


if __name__ == "__main__":
    app.run(debug=True)

When I run this and visit http://localhost:5000/help, it returns with an Error 404, Unknown URI.
image

If I comment out the line hops: hs.HopsFlask = hs.Hops(app) it works as expected:

image

When I try to run a different app (app2), that doesn’t initialize the Hops Component, it can also work fine, but I can’t seem to get two different Flask apps to run concurrently via threading or multiprocessing.

Do you know how I can get the routes to work properly, or alternatively, have any suggestions for running multiple Flask apps at the same time from the same script?

Much appreciated!
Thanks,
Joseph

1 Like

Update: I made a minor change to hopsflask.py so that it passes the original request through to the controller:

Would need to build out some better handling, but changing lines 32-39 seems to work for me for now:

        if method == "GET":
            res, results = self.query(uri=comp_uri)
            if res:
                response = self._prep_response()
                response.data = results
                return response(environ, start_response)
            else:
                return self.wsgi_app(environ, start_response)

Will update when I come up with something better.

Thanks,
Joseph

@eirannejad is this something we need to change in ghhops-server? I think it is the same as

Hi, is there any update on this issue?

I require this functionality myself, and the fix @jfreund proposed does work.

I also ran into another issue when having a POST request that I would think is probably similar, but a similar fix doesn’t work.
I’m using Dash combined with Flask, and I have a callback function to navigate to other pages.
However, the callback request results in a POST request that goes into Hops, and in there it’s not able to find the hops component because the endpoint it’s looking for is /_dash-update-component, so it returns an error. So I tried a similar fix, by doing instead:

        elif method == "POST":
            data = request.data
            res, results = self.solve(uri=comp_uri, payload=data)
            if res:
                response = self._prep_response()
                response.data = results.encode(encoding="utf_8")
            else:
                response = self._prep_response(404, "Execution Error")
                return self.wsgi_app(environ, start_response)
            return response(environ, start_response)

However, when calling wsgi_app, it’s for some reason no longer able to load the request object, and it gets stuck trying to load it, as can also be seen here that it’s trying to fetch:

You can find the basics of my app below:

from flask import Flask
from flask_bootstrap import Bootstrap
import ghhops_server as hs
from dash import Dash, html, Output, Input, dcc
import dash_bootstrap_components as dbc

EXTERNAL_STYLESHEETS = [dbc.themes.BOOTSTRAP]
server = Flask(__name__)
app = Dash(
    server=server,
    url_base_pathname='/base/',
    external_stylesheets=EXTERNAL_STYLESHEETS)

hops = hs.Hops(server)
Bootstrap(server)

navbar = dbc.Navbar(
    dbc.Container(
        [
            html.A(
                dbc.Row(
                    [
                        # dbc.Col(html.Img(src=APP_LOGO, height='50px')),
                        dbc.Col(dbc.NavbarBrand('Polar Bear', className="ms-2"), style={'fontWeight': 'bold'})
                    ],
                    align='center'),
                href='/base/home/'),
            dbc.NavItem(dbc.NavLink("Training Data", href='/base/training_data/'))
        ]),
    color='dark',
    dark=True
)

app.layout = html.Div([
    dcc.Location(id='url', refresh=False),
    navbar,
    html.H1("Base page"),
    html.Div(id='page-content')
])

@app.callback(
    Output('page-content', 'children'),
    [Input('url', 'pathname')])
def display_page(pathname):
    if pathname == '/base/training_data/':
        return html.Div(children=[html.H1("Training data page")])
    else:
        return html.Div(children=[html.H1("Home page")])


if __name__ == "__main__":
    server.run(debug=True)

I may misunderstand the fix for this but I have the same issue.

app = Flask(__name__)
hops = hs.Hops(app)

@app.route("/help",  methods=['GET'])
def help():
    return "Welcome to Grashopper Hops for CPython!"

I’m still getting the same page unless i comment out : hops = hs.Hops(app)

@eirannejad do you know why the above route is not working?

@stevebaer @nbarnes
Oops this is a regression. I’ll fix and will publish a new version soon.

2 Likes

@nbarnes We updated the package to 1.5.3

I’d appreciate if you can test now :smiley:

1 Like

Hey @eirannejad additional @routes are working for me now! thanks!

1 Like

Perfect. Thanks for testing and sorry for the bugs :smiley: