Coder Social home page Coder Social logo

jwg4 / flask-selfdoc Goto Github PK

View Code? Open in Web Editor NEW

This project forked from acoomans/flask-autodoc

18.0 4.0 4.0 331 KB

Flask-selfdoc automatically creates an online documentation for your flask application.

License: MIT License

Python 54.89% HTML 45.11%
flask documentation rest-api

flask-selfdoc's Introduction

Flask-Selfdoc

Flask-Selfdoc is a Flask extension that automatically creates documentation for your endpoints based on the routes, function arguments and docstrings. It was forked from Flask-Autodoc, written by Arnaud Coomans, and is completely compatible as a replacement for that extension. See the section on 'Compatibility' below.

Build Status Pypi version PyPI Pypi license Python 3

Requirements

Flask-Selfdoc is compatible with Python versions >=3.6 and Flask versions >=1.0. (Currently it appears that Flask 1.0 itself does not work with Python 3.10, so this combination is not supported.)

The package is tested with a selection of those versions. If you find it doesn't work correctly with versions in those ranges please report a bug, thanks.

Previous versions of Flask-Selfdoc worked with Python 2.7, Python 3.5 and Flask 0.11 and 0.12. If you need a version which works with these old versions of Python and Flask, try Flask-Selfdoc 1.2.3 (but you are better off upgrading away from end-of-life Python!!!) (Flask versions before 2.0.0 worked with Python 2.7 and Python 3.5)

Install

To install Flask-Selfdoc, run pip:

pip install flask-selfdoc

or clone this directory and run setup:

python setup.py install

or install via GitHub directly:

pip install git+https://github.com/jwg4/flask-selfdoc.git@master

Usage

Start using Flask-Selfdoc by importing it and initializing it:

from flask import Flask
from flask_selfdoc import Autodoc

app = Flask(__name__)
auto = Autodoc(app)

by default, Flask-Selfdoc will only document the routes explicitly decorated with doc:

@app.route('/user/<int:id>')
@auto.doc()
def show_user(id):
    return user_from_database(id)

to generate the documentation, use the html() method:

@app.route('/documentation')
def documentation():
    return auto.html()

Compatibility

If your codebase uses Flask-Autodoc, you can swap it for Flask-Selfdoc by simply changing the name of the module in your import:

from flask_selfdoc import Autodoc

instead of

from flask_autodoc import Autodoc

No other changes are necessary. Flask-Selfdoc 1.0 has exactly the same functionality as Flask-Autodoc 0.1.2, the most recent release at the time of the fork. Later versions of Flask-Selfdoc have added functionality but are still compatible with code that worked with Flask-Autodoc.

You can't import Flask-Selfdoc using the old naming scheme for Flask extensions, flask.ext.foo. This is deprecated by Flask and now fails to work on Flask 1.0.2. See the next section for details.

ImportError with Flask 1.0.2

If you try to run Flask-Autodoc with Flask 1.0.2, you will get an ImportError which looks like this:

Traceback (most recent call last):
  ...
ImportError: No module named ext.autodoc.autodoc

This is because Flask no longer supports modules which are named flask.ext.foo. Even if you import Autodoc as above (from flask_autodoc instead of flask.ext.autodoc) the Flask-Autodoc code internally uses the old format and so the import will fail. In versions of Flask prior to 1.0.2, Flask-Autodoc caused a warning but did not fail. Flask-Selfdoc's first and main change from Flask-Autodoc was to fix this import so that it works with Flask 1.0.2, and so that the warning is not present with older versions.

If you imported Flask-Autodoc using the old syntax,

from flask.ext.autodoc import Autodoc

you should replace it with the new syntax.

 from flask_selfdoc import Autodoc

Changing from Flask-Selfdoc to Flask-Autodoc and changing your imports as above if needed, will mean that your code will work with Flask 1.0.2 and (hopefully) all future versions.

Custom documentation

To access the documentation without rendering html:

auto.generate()

the documentation will be returned as a list of rules, where each rule is a dictionary containing:

  • methods: the set of allowed methods (ie ['GET', 'POST'])
  • rule: relative url (ie '/user/<int:id>')
  • endpoint: function name (ie 'show_user')
  • docstring: docstring of the function
  • args: function arguments
  • defaults: defaults values for the arguments

This means you can pass the output of auto.generate() to your own Jinja template.

It's also possible to do:

@app.route('/documentation/json')
def documentation():
    return jsonify(auto.generate())

However, there's actually a much better way to produce JSON data about your Flask endpoints.

@app.route('/documentation/json')
def documentation():
    return auto.json()

The structure that this returns is designed to serialize well to json, whereas the output of auto.generate() is designed to work well with HTML templates.

But the point is that you can do anything you like with the output of auto.generate()!

Custom template

To use a custom template for your documentation, give a template argument to the html method. This will use a template from the flask templates directory.

Additional arguments (other than group, groups, and template) will be passed down to the template:

auto.html(
	template='custom_documentation.html'
	title='My Documentation',
	author='John Doe',
)

title and author will be available in the template:

<!-- templates/custom_documentation.html -->
...
{% if title is defined %}
	{{title}}
{% endif %}
...

Custom fields

You can add extra keyword arguments to the doc() decorator and these will be available in the template. For example, if you have an endpoint

@app.route('/Hello/<int:id>')
@auto.doc('public', status="deprecated")
def say_hello(id):
    return "Hello"

then in your Jinja template you can access the field status

{% for doc in autodoc %}
    <div class="mapping">
        <a id="rule-{{doc.rule|urlencode}}" class="rule"><h2>{{doc.rule|escape}}</h2></a>
        <p class="docstring">{{doc.docstring|urlize|nl2br|safe}}</p>
        <p class="status">{{doc.status|safe}}</p>
    </div>
{% endfor %}

Note that Selfdoc doesn't care about or process this value in any special way, it just makes it available in the output.

Documentation sets

Endpoints can be grouped together in different documentation sets. It is possible, for instance, to show some endpoints to third party developers and have full documentation for primary developers.

To assign an endpoint to a group, pass the name of the group as argument of the doc decorator:

@app.route('/user/<int:id>')
@auto.doc('public')
def show_user(id):

to assign an endpoint to multiple groups, pass a list of group names as the groups argument to doc:

@app.route('/user/<int:id>')
@auto.doc(groups=['public','private'])
def show_user(id):

to generate the documentation for a specific group, pass the name of the group to the html or generate methods:

auto.html('public')
auto.html(groups=['public','private'])
auto.generate('public')

Examples

Apps in the examples directory are an api for a blog:

  • simple is a simple app
  • factory uses blueprints

Run with

python simple/blog.py

and connect to /doc/public and /doc/private to see public and private documentations.

Screenshots

screenshots

screenshots

Acknowledgements

Thanks to @acoomans for creating the original Flask-Selfdoc package!

Thanks to the people who built Flask and who I learnt how to do Flask from, especially @mitsuhiko, @miguelgrinberg and @ncocacola!

Thanks to the people who tried out this package and helped by posting questions and bug reports!

flask-selfdoc's People

Contributors

ballisticbuddha avatar jwg4 avatar lukeyeager avatar valentinfrancois avatar wshayes avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar

flask-selfdoc's Issues

Change the author.

acoomans should be crediting for writing most of the code, while making it clear that no-one should approach him for issues with this fork.

Make it work on Python 3.10

Currently fails because of:
ImportError: cannot import name 'MutableMapping' from 'collections'

Note: this is only with Flask 1. We should maybe just drop support for this version of Flask

Question about Form Data

Hello

In the 2nd example picture (/doc/private) you have a Form Data section under the Description for the route /post.
I checked the examples, but i can get it to work for me.
I have a template with this

<div class="desc">
                Description : {{doc.docstring}}
</div>

that display the content of what i put between """ ... """ in my code, and in your example you do this :

 """Create a new post.
    Form Data: title, content, authorid.
    """

'Create a new post ' i corectly going in the description and 'Form Data' i going to form data, however when i do the same thing, both go into the description.

How did you manage to add the FormData rule ?

Thanks

Link to documentation and other setup.py stuff

The setup.py description (and thus the PyPI description) has to link to the documentation, website (if there is one) and there must be a link to automatically install the development version (PackageName==dev). (moved to another issue)

See #13 checklist

Regression tests on flask-autodoc output.

We should check that the output is byte-for-byte the same between flask-autodoc and flask-selfdoc. We should put these checks in version 1.1 and make sure that they stay until version 2.0 at least.

And btw is this library compatible with flask-restful?

And btw is this library compatible with flask-restful?

Originally posted by @jwaxy in #72 (comment)

If I understand the issue, you want to use this extension and see the flask restful auto-generated endpoints in the data it returns?

Don't know why it wouldn't work, but never tested it. I will try it, or let me know if you try it and find something out.

enable format doc

it would be nice if selfdoc was able to support formatted doc, ala:

"""
Point your browser to {}
""".format(host_url)

Be accepted as an official Flask extension.

  • An approved Flask extension requires a maintainer. In the event an extension author would like to move beyond the project, the project should find a new maintainer including full source hosting transition and PyPI access. If no maintainer is available, give access to the Flask core team.
  • An approved Flask extension must provide exactly one package or module named flask_extensionname.
  • It must ship a testing suite that can either be invoked with make test or python setup.py test. For test suites invoked with make test the extension has to ensure that all dependencies for the test are installed automatically. If tests are invoked with python setup.py test, test dependencies can be specified in the setup.py file. The test suite also has to be part of the distribution.
  • APIs of approved extensions will be checked for the following characteristics:
    an approved extension has to support multiple applications running in the same Python process.
    it must be possible to use the factory pattern for creating applications.
  • The license must be BSD/MIT/WTFPL licensed.
  • The naming scheme for official extensions is Flask-ExtensionName or ExtensionName-Flask.
  • Approved extensions must define all their dependencies in the setup.py file unless a dependency cannot be met because it is not available on PyPI.
  • The documentation must use the flask theme from the Official Pallets Themes.
  • The setup.py description (and thus the PyPI description) has to link to the documentation, website (if there is one) and there must be a link to automatically install the development version (PackageName==dev).
  • The zip_safe flag in the setup script must be set to False, even if the extension would be safe for zipping.
  • An extension currently has to support Python 3.4 and newer and 2.7.

Return args as list

When using the generate() method and there are no arguments, args is a list:

{
	'methods': ['OPTIONS', 'POST'],
	'rule': '/auth',
	'endpoint': 'auth',
	'docstring': None,
	'args': ['None'],
	'defaults': None,
	'location': {
		'filename': '',
		'line': 0
	}
}

However, if there are arguments, args is a set:

{
	'methods': ['GET', 'HEAD', 'OPTIONS'],
	'rule': '/user/<user>',
	'endpoint': 'view_user_username',
	'docstring': None,
	'args': {'user'},
	'defaults': None,
	'location': {	
		'filename': '',
		'line': 0
	}
}

Sets are not valid JSON, and using jsonify(auto.generate()) (as shown in the documentation) results in:

TypeError: Object of type set is not JSON serializable

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.