Connexion: [question] How to serve static files ?

Created on 20 Apr 2017  路  14Comments  路  Source: zalando/connexion

question

Most helpful comment

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')

All 14 comments

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).

@fabito is this answering your question?

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 yes, the Cookbook section sounds good. Willing to do a PR?

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

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 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 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:
Upload Form/body>

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')
```

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

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

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

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.

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')

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

Was this page helpful?
0 / 5 - 0 ratings