An API, or Application Programming Interface, is a set of rules and protocols for building and interacting with software applications. APIs enable different software programs to communicate with each other, allowing developers to access functionality provided by other services without needing to understand the underlying code. It's like a menu in a restaurant; the menu provides a list of dishes you can order, along with a description of each dish. When you specify what you would like to eat, the kitchen (system) prepares the meal and serves it. In this analogy, the API is the menu, and the service provided by the kitchen is the underlying code that processes the API calls.
APIs are incredibly useful because they allow developers to use functionalities that have already been implemented by others rather than having to create them from scratch. Here's why this is beneficial:
To install wikipedia, in your terminal please run the command
pip install wikipedia-api
To use the wikipedia api in your project, import the library in your code like this:
import wikipediaapi
The Wikipedia API allows developers to access Wikipedia's vast repository of knowledge programmatically. The provided Python script uses the wikipediaapi
library to interact with the Wikipedia API, fetching information from Wikipedia articles in a structured format.
wiki_wiki.page
In the script, the wiki_wiki
variable is an instance of the Wikipedia API wrapper. The .page
method is used to retrieve a Wikipedia page corresponding to a given search term. It returns an object that includes various properties of the page, such as its content, summary, and metadata. If the page doesn't exist, it allows the program to handle this case gracefully.
In wikipedia.py, we created the Wikipedia wrapper:
# Initialize Wikipedia API wrapper
wiki_wiki = wikipediaapi.Wikipedia('english')
The script uses PySimpleGUI to create an interactive graphical user interface (GUI) that makes it easy for users to enter search terms and view Wikipedia summaries. Here's an overview of the key components:
if search_term:
summary = get_wikipedia_summary(search_term)
window['-OUTPUT-'].update(summary)
The code snippet is part of a Python script using PySimpleGUI to create a graphical user interface for a Wikipedia search tool. Here is what each line does:
if search_term:
checks if the variable search_term
is truthy, meaning it checks if the user has entered a term in the input field. If search_term
is not empty or None
, the condition evaluates to True
and the code inside the if-block will execute.
summary = get_wikipedia_summary(search_term)
calls the function get_wikipedia_summary
with the user-provided search_term
as an argument. The function queries the Wikipedia API and returns the summary of the article related to the search term.
window['-OUTPUT-'].update(summary)
updates the GUI component with the key '-OUTPUT-'
, which is a multiline text box, with the summary received from the Wikipedia API. This allows the summary to be displayed on the screen for the user to read.
In summary, this snippet takes the user's input, retrieves a summary of the relevant Wikipedia page, and displays it in the application's window.
When a user enters a term and clicks the 'Search' button, the script uses the Wikipedia API to fetch a summary of the related article. This summary is then displayed in the GUI for the user to read. The 'Exit' button allows the user to close the application at any time.
This combination of a simple-to-use GUI and the powerful Wikipedia API provides a user-friendly way to quickly access information from one of the world's largest knowledge databases. For more information about the Wikipedia API, feel free to read the documentation here.