Posts

Showing posts with the label flask

Autocomplete Using Redis and Python

Autocomplete using Redis and Python Reading about the use cases of the Redis I came across a use case in which we can implement an autocomplete functionality using it. I am going to show you the code to implement the Autocomplete in under 40 lines of code. Here we go: #redis client for python import redis #flask to expose api's to outside world from flask import Flask,request,jsonify app = Flask("autocomplete") #creating a redis connection r = redis.StrictRedis(host='localhost', port=7001, db=0) #route to add a value to autocomplete list ''' FORMAT: localhost:5000/add?name=<name> ''' @app.route('/add') def add_to_dict(): try: name = request.args.get('name') n = name.strip() for l in range(1,len(n)): prefix = n[0:l] r.zadd('compl',{prefix:0}) r.zadd('compl',{n+"*":0}) return "Added"