Construct Symbol Seek App with Python and Flask
In our earlier Python Flask instructional, we now have defined broaden QR Code Generator with Python and Flask. On this instructional, we can give an explanation for broaden Symbol Seek App with Python and Flask.
A picture seek is a seek capability this is applied to search out pictures. The quest will also be in line with key phrases, an image, or a internet hyperlink to a picture.
Right here on this instructional, we can create an consumer pleasant interface for looking out pictures with key phrase and put into effect capability for picture seek the use of Unsplash API with Python and Flask.
So let’s continue with growing Symbol Seek App venture:
1. Mission Setup and Module Set up
First we can create our venture image-search-python-flask listing .
$ mkdir image-search-python-flask
and moved to the venture.
$ cd image-search-python-flask
Then we can set up required modules for our utility. As we can broaden internet primarily based utility, so we can set up Flask micro framework module to create internet utility.
pip set up Flask
2. Create Flask Software
We will be able to create our venture python report app.py and import required modules.
from flask import Flask, render_template, request, redirect, url_for import sys import os import requests
Then we can create our Flask app and run app.
app = Flask(__name__) app.secret_key = '1234567890' if __name__ == '__main__': app.run()
3. Create Symbol Seek Shape
Now we can create template report for our flask utility. We will be able to create templates listing in our venture and create index.html template report. Then we can create seek shape to go looking picture with key phrases.
4. Put in force Symbol Seek with API
Then we put into effect serve as index() in app.py and put into effect capability to go looking picture with consumer inputted key phrase. We will be able to test for shape post and get key phrase after which make unsplash API request with params to go looking pictures from API. Then after all go back the hunt end result with to template report to show seek end result.
@app.path('/', strategies=['GET', 'POST']) def index(): searchResult="" message="" searchQuery = '' clientId = 'y4i-BtdyHO0mBSE7BhqVPgCBFhqbaTtruS9oZmyKMho' if request.way == 'POST': searchQuery = str(request.shape['searchQuery']) if searchQuery: apiUrl="https://api.unsplash.com/seek/pictures" params = {'question': searchQuery, 'client_id': clientId} reaction = requests.get(apiUrl, params=params, allow_redirects=True) searchResult = reaction.json() else: message="Please input seek key phrase" go back render_template('index.html', message = message, searchQuery = searchQuery,searchResult = searchResult)
5. Show Symbol Seek Outcome
In any case, in index.html template report, we can show the hunt end result.
{% if searchResult %} {% for picture in searchResult.effects %}{% endfor %} {% endif %}