Flask App Route + Hops


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)

def help():
    return "Welcome to Grasshopper Hops for CPython!"

if __name__ == "__main__":

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

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


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!

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)
                return self.wsgi_app(environ, start_response)

Will update when I come up with something better.


@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")
                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

server = Flask(__name__)
app = Dash(

hops = hs.Hops(server)

navbar = dbc.Navbar(
                        # dbc.Col(html.Img(src=APP_LOGO, height='50px')),
                        dbc.Col(dbc.NavbarBrand('Polar Bear', className="ms-2"), style={'fontWeight': 'bold'})
            dbc.NavItem(dbc.NavLink("Training Data", href='/base/training_data/'))

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

    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")])
        return html.Div(children=[html.H1("Home page")])

if __name__ == "__main__":

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.


@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: