Skip to main content

Flask Kafka

⚠️ Breaking Changes: Dropping kafka-python library in favour of confluent-kafka due it's support & documentation

This is an easy to use utility to help Flask developers to implement microservices that interact with Kafka. This library has been inspired by two other similar libraries :-

After looking around the web and on Github, I was not able to find a lot of content on how to consume from a Kafka topic using the Kafka framework. From what I found, I was able to come up with this library by borrowing from the above libraries. They both had a little of what I wanted so I combined them to come up with this one.

I hope you find this useful.

Features

  • Doesn't block process
  • Configure by config.py
  • Support comsuming from topic by decorator

Installation

This project has been commited to Pypi, can be installed by pip:

$ pip install flask-kafka

Simple example

from flask import Flask, request

from flask_kafka import FlaskKafka
app = Flask(__name__)
app.config["KAFKA_CONFIG"] = {'bootstrap.servers': 'localhost:9092',
                              'group.id': 'foo',
                              'enable.auto.commit': 'false',
                              'auto.offset.reset': 'earliest'}

bus = FlaskKafka()
bus.init_app(app)

# curl http://localhost:5004/publish/test-topic?key=foo&value=bar

@app.route('/publish/<topic>', methods=["get"])
def publish(topic):
    qstr = request.args.to_dict()
    key = qstr['key']
    value = qstr['value']
    publisher = bus.get_producer()
    publisher.produce(topic, key=key, value=value)
    publisher.poll(1)
    return "Published to {} => {} : {}".format(topic, key, value)

@bus.handle('test-topic')
def test_topic_handler(consumer,msg):
    print("Consumed event from topic {topic}: key = {key:12} value = {value:12}".format(
            topic=msg.topic(), key=msg.key().decode('utf-8'), value=msg.value().decode('utf-8')))


# or
# bus.add_topic_handler("test-topic", lambda consumer, msg: print(msg.value()))

if __name__ == '__main__':
    bus.run()
    app.run(debug=True, port=5004, use_reloader=False)

Special Thanks

License

MIT License

Copyright (c) 2019 Nimrod Kevin Maina

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.

Metadata

Release files for flask-kafka 0.1.0

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for flask-kafka 0.1.0
File Size Uploaded Provenance Actions
flask-kafka-0.1.0.tar.gz 6.8 kB Without provenance Details for flask-kafka-0.1.0.tar.gz

Built distribution (wheel)

Table of built distributions (wheels) for flask-kafka 0.1.0
File Interpreter ABI Platform Actions
flask_kafka-0.1.0-py3-none-any.whl Python 3 none any Details for flask_kafka-0.1.0-py3-none-any.whl

Total release size: 14.0 kB

Release files / flask-kafka-0.1.0.tar.gz

Download URL flask-kafka-0.1.0.tar.gz
Size 6.8 kB
Tags Source
SHA-256 checksum
How to use checksums
1a0b84ec4eb099be56c8bfde0a79957db818ae56f0485413c0cd94e3943f5f7f
BLAKE2b-256 checksum
How to use checksums
63fe43b714cb2cee71dfeff01d23b11de6970f540659e5d4b96482882d4a6fe2
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/4.0.2 CPython/3.9.13

Release files / flask_kafka-0.1.0-py3-none-any.whl

Download URL flask_kafka-0.1.0-py3-none-any.whl
Size 7.1 kB
Tags Python 3
SHA-256 checksum
How to use checksums
b0d04281b0add214dc7497ec6583e88f5b1561385ce48ea6f68926620c7f0901
BLAKE2b-256 checksum
How to use checksums
99622448185cca873eb8e8f27d5dda413adfaa31f24cf64fbaf07403cd8d29df
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/4.0.2 CPython/3.9.13

Security for flask-kafka 0.1.0

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

No provenance found

None of the files in this release were uploaded with provenance metadata.

A note on package security

PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.

To learn more about protecting yourself from malicious actors, visit our package security documentation.

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page