Dapr Python SDK integration with Flask

How to create Dapr Python virtual actors with the Flask extension

The Dapr Python SDK provides integration with Flask using the flask-dapr module

Installation

You can download and install the Dapr Flask extension module with:

  1. pip install flask-dapr

Note

The development package will contain features and behavior that will be compatible with the pre-release version of the Dapr runtime. Make sure to uninstall any stable versions of the Python SDK extension before installing the dapr-dev package.

  1. pip install flask-dapr-dev

Example

  1. from flask import Flask
  2. from flask_dapr.actor import DaprActor
  3. from dapr.conf import settings
  4. from demo_actor import DemoActor
  5. app = Flask(f'{DemoActor.__name__}Service')
  6. # Enable DaprActor Flask extension
  7. actor = DaprActor(app)
  8. # Register DemoActor
  9. actor.register_actor(DemoActor)
  10. # Setup method route
  11. @app.route('/GetMyData', methods=['GET'])
  12. def get_my_data():
  13. return {'message': 'myData'}, 200
  14. # Run application
  15. if __name__ == '__main__':
  16. app.run(port=settings.HTTP_APP_PORT)

Last modified January 1, 0001