connexion icon indicating copy to clipboard operation
connexion copied to clipboard

[question] How to serve static files ?

Open fabito opened this issue 9 years ago • 14 comments

fabito avatar Apr 20 '17 01:04 fabito

You can use the normal Flask tools to service static files (app.app is a Flask app), e.g. use flask.send_from_directory(..) like Connexion itself does to serve the Swagger UI files (https://github.com/zalando/connexion/blob/master/connexion/apis/flask_api.py#L291).

hjacobs avatar Apr 20 '17 07:04 hjacobs

@fabito is this answering your question?

hjacobs avatar Apr 20 '17 22:04 hjacobs

May a Howto section in the documentation would be nice for examples like this :) [Update:] Ok, there is a Cookbook section where this could fit.

webwurst avatar May 20 '17 14:05 webwurst

@webwurst yes, the Cookbook section sounds good. Willing to do a PR?

hjacobs avatar May 21 '17 07:05 hjacobs

So how is this done anyhow?

I'm using connexion with swagger and I have the following

app = connexion.App(__name__)
app.add_api('swagger.yaml')

but neither app, nor app.app have flask methods such as send_from_directory

felipemullen avatar Oct 13 '17 21:10 felipemullen

For anyone who comes across this issue from google: I did an import of flask from my api module to serve the homepage (even though the flask app is instantiated in a different file), and served the rest of the files from nginx

api.py:

import flask

def go_home():
    """
    GET /
    Serves the home page
    """

    return flask.send_from_directory('public', 'index.html')

felipemullen avatar Oct 16 '17 15:10 felipemullen

@felipemullen what did you add in your swagger.yaml to allow file send? Im trying to create a small website in the same server as my json API

Do you have an example, where you add the go_home() to the router of flask?

jonasman avatar Apr 26 '18 12:04 jonasman

@jonasman I added the following, to allow me to server html pages, to my swagger.yml file.

/:
  get:
    tags: 
      - RandomTag
    summary: Displays static page.
    description: Displays the index html page
    operationId: go_home
    produces:
      - text/html
    responses:
      '200':
        description: Successfully loaded html page.
        examples:
          text/html: 
            <html><body>Upload Form/body></html>

And then in the random_tag_controller.py, where my html file was stored in frontend folder.

import flask

def go_home():
    return flask.send_from_directory('frontend', 'index.html')

hmajid2301 avatar May 29 '18 11:05 hmajid2301

For posterity, I ended up going this way to segment my front and back ends more completely. http://flask.pocoo.org/docs/1.0/patterns/appdispatch/#combining-applications

chrisbrake avatar Nov 08 '18 19:11 chrisbrake

Another approach i would recommend is to use your production web server (e.g. nginx) to serve your static content.

dtkav avatar Nov 08 '18 19:11 dtkav

@hmajid2301, Thanks! I also need to fix issue with Werkzeug: attempt implicit sequence conversion but the response object is in direct passthrough mode:

imort flask 


def get(filename):
    response = flask.send_from_directory('static', filename)
    response.direct_passthrough = False
    return response

In schema I use:

'static/{filename}':
  get:
    parameters:
      - name: filename
        in: path
  # and so on

Use openapi 3.0.0, connexion==2.0.2, Werkeug==0.14.1.

Intey avatar Dec 17 '18 15:12 Intey

A solid example of how to serve static content, with or without catchall, would be greatly appreciated. I've spent an hour trying to follow what's stated here without any success. I've done it before in Flask, but how to access flask "Correctly" using the proper connexion methodology doesn't work either.

I realize this is an API system and not designed for static, but sometimes you need to serve one or two small static items to make life better.

ShakataGaNai avatar Feb 01 '19 21:02 ShakataGaNai

For someone come here like me using openapi 3 rather than swagger 2:

Swagger.yaml:

/:
  get:
    tags: 
      - RandomTag
    summary: Displays static page.
    description: Displays the index html page
    operationId: go_home
#    produces:    # "produces" is not available in openapi 3 any more.
#      - text/html
    responses:
      '200':
        description: Successfully loaded html page.
        content:
          text/html:   # Add the media type here
            schema:
              type: string
        examples:
          text/html: 
            <html><body>Upload Form/body></html>

controller file:

import flask

def go_home():
    return flask.send_file('frontend/index.html')

XuJiawan avatar Mar 07 '19 13:03 XuJiawan

One other possibility is to use Whitenoise to serve the static files

lioman avatar Jul 11 '19 10:07 lioman