question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Recommended way to serve .yaml/.json

See original GitHub issue

I’m trying to find the best way to serve locally my swagger.json and set it up to get_swaggerui_blueprint. I’m not Flask fluent and the project I’m working on configured a resource that read the json file, load it into a dict and then returns it. Surely there is a better way.

I believe would be better to have an example serving the swagger.json locally on the README.md, since it’s probably the most common use.

Issue Analytics

  • State:closed
  • Created 6 years ago
  • Reactions:1
  • Comments:5 (2 by maintainers)

github_iconTop GitHub Comments

8reactions
CaveSvencommented, Jan 8, 2018

For a static yaml file you can do

from flask_swagger_ui import get_swaggerui_blueprint
from yaml import Loader, load
from settings import SWAGGER_URL, swagger_path

swagger_yml = load(open(swagger_path, 'r'), Loader=Loader)

blueprint = get_swaggerui_blueprint(SWAGGER_URL, swagger_path, config={'spec': swagger_yml})
4reactions
sveintcommented, Sep 21, 2017

Sorry, yeah, I meant API documentation.

In principle they are decoupled. For instance, in the application where I use this package myself, the swagger json is dynamically created from the REST API endpoints’ documentation. I don’t have an actual file on my system. I would imagine this being common for many (most?) people wanting to use this package.

Also, there can other tools using the swagger.json, which are not related to this package at all.

Creating a resource to serve the file can be as simple as creating a dumb flask resource:

# Import flask and create app, then:

@app.route('/api/swagger.json')
def swagger_json():
    # Read before use: http://flask.pocoo.org/docs/0.12/api/#flask.send_file
    return send_file('path/to/swagger.json')  

swaggerui_blueprint = get_swaggerui_blueprint(
    SWAGGER_URL,
    '/api/swagger.json',
    ...
)
app.register_blueprint(swaggerui_blueprint, url_prefix=SWAGGER_URL)

Have you tried this? Is there anything preventing this from working?

Read more comments on GitHub >

github_iconTop Results From Across the Web

YAML and JSON - Nubenetes
How to create Kubernetes YAML files; Kubernetes examples; Helm and Kustomize. JSON. ... We recommend using an API from a programming language instead...
Read more >
10 YAML tips for people who hate YAML | Enable Sysadmin
In this method, you write in JSON and then run a Python conversion script to produce YAML. This scales better than self-conversion, because...
Read more >
What is the difference between YAML and JSON?
Technically YAML is a superset of JSON. This means that, in theory at least, a YAML parser can understand JSON, but not necessarily...
Read more >
YAML Tutorial: Everything You Need to Get Started in Minutes
Before we take a deeper dive, let's look at how this document looks in JSON. I'll throw it in this handy JSON to...
Read more >
Working in YAML (OpenAPI tutorial) - Idratherbewriting.com
Many computers ingest data in a YAML or JSON format. It's a syntax commonly used in configuration files and an increasing number of...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found