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

Added configuration for swagger-ui and jquery js external-load URLs. #245

Merged
merged 1 commit into from
Sep 18, 2018

Conversation

javabrett
Copy link
Collaborator

@javabrett javabrett commented Sep 18, 2018

This change allows new Flasgger config keys to specify the URL location where Swagger UI and jQuery javascript sources are loaded from, allowing them to be loaded from Flasgger's static copies (default), or any URL, including hosted sources. This can allow any compatible version of those libraries to be loaded, allowing the latest Swagger UI to be loaded by Flasgger templates.

The new, optional config keys are:

  • swagger_ui_bundle_js
  • swagger_ui_standalone_preset_js
  • jquery_js

For example, the following can be used to load scripts from unpkg.com:

app = Flask(__name__)
app.config['SWAGGER'] = {
    'title': 'Colors API'
}
swagger_config = Swagger.DEFAULT_CONFIG
swagger_config['swagger_ui_bundle_js'] = '//unpkg.com/swagger-ui-dist@3/swagger-ui-bundle.js'
swagger_config['swagger_ui_standalone_preset_js'] = '//unpkg.com/swagger-ui-dist@3/swagger-ui-standalone-preset.js'
swagger_config['jquery_js'] = '//unpkg.com/jquery@2.2.4/dist/jquery.min.js'
Swagger(app, config=swagger_config)

As part of this change, template part flasgger/body_scripts.html was factored-out of index.html, making it easier to override and replace the body script load section entirely if desired.

Added a test/example.

This is somewhat inspired by discussions in #231, so I mention it here.

Questions

  • Is the colors example a good base for new test/example code? That's what I copied for the test/example. I wonder if we should have a canonical, minimal, template example app that forms the base for tests which need to exercise one function without testing unwanted features or copying a lot of example code.
  • I exposed three new config keys. I was wondering if there was a more generic way of doing this, or allowing override, other than the addition of the factored-out body_scripts.html template.
  • Check config key names - I didn't include url in them.

Testing

  • Access the new colors_external_js example with a browser in developer mode and note where the scripts are fetched from. The example is using a floating/latest ui3 mechanism, so there is a redirect involved. This loads the latest Swagger UI 3.x, which currently loads 3.19.0.

TODO

  • I notice I need to externalise the CSS link too.

@coveralls
Copy link

coveralls commented Sep 18, 2018

Coverage Status

Coverage remained the same at 91.184% when pulling 1467de3 on static-js-url-config into 6739622 on master.

…d URLs.

This change allows new Flasgger config keys to specify the URL location where jQuery and Swagger javascript sources,
and Swagger CSS are loaded from, allowing them to be loaded from Flasgger's static copies (default), or any URL, including hosted sources.
This can allow any compatible version of those libraries to be loaded, allowing the latest Swagger UI to be loaded by Flasgger templates.

The new, optional config keys are:

- swagger_ui_bundle_js
- swagger_ui_standalone_preset_js
- jquery_js
- swagger_ui_css

For example, the following can be used to load scripts from unpkg.com:

app = Flask(__name__)
app.config['SWAGGER'] = {
    'title': 'Colors API'
}
swagger_config = Swagger.DEFAULT_CONFIG
swagger_config['swagger_ui_bundle_js'] = '//unpkg.com/swagger-ui-dist@3/swagger-ui-bundle.js'
swagger_config['swagger_ui_standalone_preset_js'] = '//unpkg.com/swagger-ui-dist@3/swagger-ui-standalone-preset.js'
swagger_config['jquery_js'] = '//unpkg.com/jquery@2.2.4/dist/jquery.min.js'
swagger_config['swagger_ui_css'] = '//unpkg.com/swagger-ui-dist@3/swagger-ui.css'
Swagger(app, config=swagger_config)

As part of this change, template part flasgger/body_scripts.html was factored-out of index.html, making it easier to override and replace the body script load section entirely if desired.

Added a test/example.
@rochacbruno
Copy link
Member

Is the colors example a good base for new test/example code? That's what I copied for the test/example. I wonder if we should have a canonical, minimal, template example app that forms the base for tests which need to exercise one function without testing unwanted features or copying a lot of example code.

This colors app is good for me, it is simple and uses some of the features, the ideal would be having a minimal app that uses most of the features but looks like impossible to have it. I am OK if you want to add a new example app with a more minimal implementation.

I exposed three new config keys. I was wondering if there was a more generic way of doing this, or allowing override, other than the addition of the factored-out body_scripts.html template.

I think the only ways are using the scripts + config variables, which is already in place, I cannot think of anything different than that. (for more customization, users will need to rewrite the whole template)

@javabrett
Copy link
Collaborator Author

@rochacbruno thanks for the review. I'm good with the colors example for now, and the variables seem OK too.

Let me know if you have any changes or will review further or are +ve or -ve on this sort of change. I think it should make it fairly easy for flasgger users to choose updated Swagger UI versions.

@rochacbruno
Copy link
Member

@javabrett LGTM

@javabrett javabrett merged commit 490bcc0 into master Sep 18, 2018
@javabrett javabrett deleted the static-js-url-config branch September 18, 2018 23:00
@javabrett
Copy link
Collaborator Author

For anyone interested, there's now a live example of this deployed to http://flasgger.pythonanywhere.com/colors_external_js/apidocs/ . This loads the latest Swagger UI 3.x into Flasgger's UI, which at the time of this post is 3.19.5.

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

Successfully merging this pull request may close these issues.

None yet

3 participants