nglviewer / ngl

WebGL protein viewer
http://nglviewer.org/ngl/
MIT License
657 stars 168 forks source link

Load PDB being hosted locally by Flask Server #953

Closed spadavec closed 1 year ago

spadavec commented 1 year ago

I'm hosting a file locally by a Flask server at a specific endpoint:

@app.route('/load_protein', methods=['GET'])
def load_protein():
    return send_file('./proteins/KRAS.pdb')

When I then try to hit this endpoint:

      return stage.loadFile('http://127.0.0.1:5000/load_protein', {'ext' : 'pdb'})

I'm getting CORS errors and can't load the PDB:

Access to XMLHttpRequest at 'http://127.0.0.1:5000/load_protein' from origin 'http://localhost:1212' has been blocked by CORS policy: No 'Access-Control-Allow-Origin' header is present on the requested resource.

Any idea on how I can load a PDB thats being hosted locally via Flask?

spadavec commented 1 year ago

For the people finding this later, it looks like you need to change the header on the response:


def _add_cors_header(response):
    response.headers.add("Access-Control-Allow-Origin", "*")
    return response

@app.route('/load_protein', methods=['GET'])
def load_protein():
    response = make_response(send_file('./proteins/KRAS.pdb'))
    return _add_cors_header(response)

And make sure that the {ext : 'pdb'} is set as a parameter in the Load stage.