User:Jeropbrenda/Sandbox/Tutorials/Holidays viewer

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.
 * MediaWiki Action API modules: API:Parse, API:Login and API:Edit.

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 app.py, 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 it's section number and the content in that section number.

Create a function to get the section number using API:Parse:

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

Update holiday links
The html of the holidays returned contains Help:Links that point to those holidays, e.g "/wiki/New_Years_Day". We need to prepend "//en.wikipedia.org" to these links using jQuery in order 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 of 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 first.

To add a holiday, send a request to API:Edit 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 list_holidays(...):

Step 8: 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

 * Got questions about this tutorial? Ask here on Discourse or on this tutorials discussion page.
 * Contribute a demo app that you have developed using the MediaWiki API to this code samples repository.