API:Holidays viewer/pl

Overview
This tutorial covers how to create a demo app that fetches holidays and observances for a given date from Wikipedia, with an option to log in to add new holidays.

Download the code from GitHub Browse the app on Toolforge

The tools and technologies used to create the demo app are:


 * Python 3 and Flask, a Python framework.
 * jQuery and Bootstrap. They are loaded from Wikimedia Toolforge for privacy reasons.
 * MediaWiki Action API modules:, and.

Step 1: Set up Python and Flask development environment
Python comes pre-installed on most Linux distributions. For other operating systems, see the Python beginner's guide for installation instructions.

Install Flask by running. If you don't have Pip, get it from the official Pip website

Step 2: Create a simple Flask application
In your home directory, create a folder named  which will contain all the app's files. Inside the folder, create a file named  and place the following code in it:

Run the app using the command  and open   on your browser. You should see "Holidays and observances" displayed.

Step 3: Create the base layout
The app will have four pages: the homepage, a search page, a login page and an add page. Each page will have some common elements, so we need to create a base layout file called  to contain these elements.

Note that we are using Bootstrap classes to apply a specific CSS style to an element, Materialize icons for the add, search and arrow-back icons, and Jinja to extend the base layout to other pages and to pass variables from Python to HTML.

Other pages will extend  using the code below:

Step 4: List holidays
The root url of the app will trigger the  function, which lists holidays for a certain date.

In the function and throughout the app,  refers to the date of the holidays to be listed,   refers to the title of the page, and   refers to the html which contains the holidays to be listed. We'll also be using the  function which renders a specific html file from the templates directory. Other arguments added to the function are variables which are being passed to the html file.

In, update   with the code below:

Get today's date
If no date is specified, we'll list holidays for today's date. To use Python's datetime module to get today's date, import the module with  then create the following function:

Call the function in :

Get the holidays to be listed
Once we have the date, we get the holidays for that date. Wikipedia has a page for each date and the holidays are under a section titled "Holidays and observances". To get the holidays, we need to get its section number and the content in that section number.

Create a function to get the section number using :

Create a function called  to get the holidays in that section using  as well, then call the functions in  :

Update holiday links
The HTML of the holidays returned contains internal links that point to those holidays, e.g " ". We need to prepend " " to these links using jQuery to make them external links in our app, and make them open in a new tab. To do that, add the following code to :

Then add jQuery to  using:

Step 5: Search for holidays of other dates
To get holidays for other dates, create a search route to display a form that collects the month and day to search for:

Once the search form has been submitted, update  to be the date that has been entered. To do that, add the following code to :

Step 6: Add a holiday
The page to which we'll be adding a new holiday is protected from edits by anonymous users, so we need to log in using API:Login#clientlogin first.

To add a holiday, send a request to with the date and description of the holiday. The edit adds new holidays to this page on Test Wikipedia: Sandbox/Holidays_and_observances. This is to prevent adding test holidays to English Wikipedia.

After the holiday is added, redirect to the homepage where the holidays added will also be shown, and formatted in bold to differentiate them from the real holidays. To fetch the test holidays alongside the real holidays, update :

Step 7: Styling the app
To add more style to our app, create a stylesheet named  and link to it from   by adding.

Application layout
At this point, the structure of your app should be:

$HOME/holidays-viewer ├── templates/ │  └── add.html └── index.html └── layout.html └── login.html └── search.html ├── static/ │  └── style.css └── update-links.js ├── app.py

With  and   being:



Next steps

 * Contribute a demo app that you have developed using the MediaWiki API to this code samples repository.



Zobacz też

 * — The quick start guide for the MediaWiki Action API.
 * — Parses the content of a page and obtain the output.
 * — Edits a page.
 * — Allows logging in to a wiki.