Open In App

Wikipedia search app using Flask Framework – Python

Last Updated : 26 May, 2022
Improve
Improve
Like Article
Like
Save
Share
Report

Flask is a micro web framework written in Python. It is classified as a micro-framework because it does not require particular tools or libraries. Flask is a lightweight WSGI web application framework. It is designed to make getting started quick and easy, with the ability to scale up to complex applications. Wikipedia is a Python library that makes it easy to access and parse data from https://www.wikipedia.org/.

Installation:

1) In order to create the flask app we have to first install the flask.

pip install flask

2) In order to extract the data from Wikipedia, we must first install the Python Wikipedia library.

pip install wikipedia

Create a flask app:

3) Create a file and name it as app.py

4) Create the templates folder to store all the html files.

Folder structure:

How to create wikipedia search app using Flask Framework?

Now, let’s start coding the application

Create the file  – app.py

Python3




# import necessary libraries
from flask import Flask, request, render_template
import wikipedia
  
app = Flask(__name__)
  
# create HOME View
@app.route("/", methods=["POST", "GET"])
def home():
    if request.method == "GET":
        return render_template("index.html")
    else:
        search = request.form["search"]
  
        # Fetch data from wikipedia
        result = wikipedia.summary(search, sentences=2)
        return f"<h1>{result}</h1>"
  
  
if __name__ == '__main__':
    app.run(debug=True)


Create a file index.html that will be used by flask – 

HTML




<!DOCTYPE html>
<html>
<head>
    <title>Wikipedia Search</title>
</head>
<body>
    <form method="post">
    <input type="text" name="search">
    <br>
    <button type="submit">Search</button>
    </form>
</body>
</html>


Output:

If we search INDIA in this input tag then the output is:



Like Article
Suggest improvement
Previous
Next
Share your thoughts in the comments

Similar Reads