Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[question] How to serve static files ? #441

Closed
fabito opened this issue Apr 20, 2017 · 14 comments
Closed

[question] How to serve static files ? #441

fabito opened this issue Apr 20, 2017 · 14 comments
Labels

Comments

@fabito
Copy link

fabito commented Apr 20, 2017

No description provided.

@hjacobs
Copy link
Contributor

hjacobs commented Apr 20, 2017

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
Copy link
Contributor

hjacobs commented Apr 20, 2017

@fabito is this answering your question?

@webwurst
Copy link

webwurst commented May 20, 2017

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.

@hjacobs
Copy link
Contributor

hjacobs commented May 21, 2017

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

@felipemullen
Copy link

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
Copy link

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

@jonasman
Copy link

jonasman commented Apr 26, 2018

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

@hmajid2301
Copy link

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

@chrisbrake
Copy link

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

@dtkav
Copy link
Collaborator

dtkav commented Nov 8, 2018

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

@Intey
Copy link

Intey commented Dec 17, 2018

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

@ShakataGaNai
Copy link

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.

@XuJiawan
Copy link

XuJiawan commented Mar 7, 2019

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

@lioman
Copy link

lioman commented Jul 11, 2019

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests