# Flask

This section describes integration with Flask web framework.

## View decorator

Flask can be integrated by view decorator to apply OpenAPI validation to your application’s specific views.

Use `FlaskOpenAPIViewDecorator` with OpenAPI object to create the decorator.

      from openapi_core.contrib.flask.decorators import FlaskOpenAPIViewDecorator

      openapi_validated = FlaskOpenAPIViewDecorator(openapi)

      @app.route('/home')
      @openapi_validated
      def home():
         return "Welcome home"

You can skip response validation process: by setting `response_cls` to `None`

      from openapi_core.contrib.flask.decorators import FlaskOpenAPIViewDecorator

      openapi_validated = FlaskOpenAPIViewDecorator(
         openapi,
         response_cls=None,
      )

If you want to decorate class based view you can use the decorators attribute:

      class MyView(View):
         decorators = [openapi_validated]

         def dispatch_request(self):
             return "Welcome home"

      app.add_url_rule('/home', view_func=MyView.as_view('home'))

## View

As an alternative to the decorator-based integration, a Flask method based views can be integrated by inheritance from `FlaskOpenAPIView` class.

      from openapi_core.contrib.flask.views import FlaskOpenAPIView

      class MyView(FlaskOpenAPIView):
         def get(self):
             return "Welcome home"

      app.add_url_rule(
         '/home',
         view_func=MyView.as_view('home', spec),
      )

Additional customization parameters can be passed to the view.

      from openapi_core.contrib.flask.views import FlaskOpenAPIView

      class MyView(FlaskOpenAPIView):
         def get(self):
             return "Welcome home"

      app.add_url_rule(
         '/home',
         view_func=MyView.as_view(
             'home', spec,
             extra_format_validators=extra_format_validators,
         ),
      )

## Request parameters

In Flask, all unmarshalled request data are provided as Flask request object’s `openapi.parameters` attribute

      from flask.globals import request

      @app.route('/browse/<id>/')
      @openapi
      def browse(id):
         browse_id = request.openapi.parameters.path['id']
         page = request.openapi.parameters.query.get('page', 1)

         return f"Browse {browse_id}, page {page}"

## Low level

You can use `FlaskOpenAPIRequest` as a Flask request factory:

    from openapi_core.contrib.flask import FlaskOpenAPIRequest

    openapi_request = FlaskOpenAPIRequest(flask_request)
    result = openapi.unmarshal_request(openapi_request)

For response factory see Werkzeug integration.