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 extension.

Installation

You can download and install the Dapr Flask extension with:

  1. pip install flask-dapr

Note

  1. 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 <code>dapr-dev</code> 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)