Flask is a framework that is widely used to create website apps in Python. Flask is a simple yet powerful web framework that is designed to help us get started quickly with the ability to scale up to complex applications.
Flask is also called a “micro web framework” because it was built with a simple but extensible core.
Wikipedia is a Python library that makes it easy to access and parse data from Wikipedia.
Here is a quick start to the Python library.
>>> import wikipedia>>> print wikipedia.summary("Wikipedia")# Wikipedia (/ˌwɪkɨˈpiːdiə/ or /ˌwɪkiˈpiːdiə/ WIK-i-PEE-dee-ə) is a collaboratively edited, multilingual, free Internet encyclopedia supported by the non-profit Wikimedia Foundation...>>> wikipedia.search("Barack")# [u'Barak (given name)', u'Barack Obama', u'Barack (brandy)', u'Presidency of Barack Obama', u'Family of Barack Obama', u'First inauguration of Barack Obama', u'Barack Obama presidential campaign, 2008', u'Barack Obama, Sr.', u'Barack Obama citizenship conspiracy theories', u'Presidential transition of Barack Obama']>>> ny = wikipedia.page("New York")>>> ny.title# u'New York'>>> ny.url# u'http://en.wikipedia.org/wiki/New_York'>>> ny.content# u'New York is a state in the Northeastern region of the United States. New York is the 27th-most exten'...>>> ny.links[0]# u'1790 United States Census'>>> wikipedia.set_lang("fr")>>> wikipedia.summary("Facebook", sentences=1)# Facebook is an online social networking service on the Internet that allows information to be published (photographs, links, texts, etc.) by controlling their visibility by different categories of people.
Let’s start by installing the neccessary dependencies for the app to work.
pip install pipenv
pipenv shell
pipenv install Flask
In order to be able to extract the data from Wikipedia, we must first install the Python Wikipedia library.
pipenv install wikipedia
Create a file and name it app.py
Create the templates folder to store all the html files
Let’s start coding.
# import necessary librariesfrom flask import Flask, request, render_template, redirect, url_forimport wikipediafrom wikipedia.wikipedia import summary# referencing the flask appapp = Flask(__name__)#home view@app.route("/", methods=["POST", "GET"])def home():#if the request is using a POST methodif request.method == 'POST':#try and except method helps to avoid crashing the app# instead it gives the error and the app continues functioningtry:#pulling out the actual input from the html formwiki = request.form['Wikipedia']#summarize the data from wikipedia#output only two sentencesdata = wikipedia.summary(wiki, sentences=2)#f string which only works for python 3.6 and abovereturn f'{data}'except Exception as e:return f'<p>{e} </p>'else:return render_template('home.html')#for the app to startif __name__ == '__main__':# inorder to turn on debug modeapp.run(debug=True)
<!DOCTYPE html><html lang="en"><head><meta charset="UTF-8"><meta http-equiv="X-UA-Compatible" content="IE=edge"><meta name="viewport" content="width=device-width, initial-scale=1.0"><title>Wikipedia search engine</title><!-- bootstrap cdn --><link href="https://cdn.jsdelivr.net/npm/bootstrap@5.1.3/dist/css/bootstrap.min.css" rel="stylesheet"integrity="sha384-1BmE4kWBq78iYhFldvKuhfTAU6auU8tT94WrHftjDbrCEXSU1oBoqyl2QvZ6jIW3"crossorigin="anonymous"></head><body><div class="container mt-4"><!-- inorder to submit the request --><form method="POST" action="." ><!-- name = wikipedia inorder to use it from the backend --><input type="text" class="form-control" name="Wikipedia"><br><button type="submit" class="btn btn-outline-primary">Search</button></form></div></body></html>
python app.py
Here is a link to the GitHub repo.