User:Jeropbrenda/Sandbox/Tutorials/Holidays viewer

Overview
This tutorial covers how to create a demo app that fetches the day's holidays and observances from Wikipedia, allows searching for holidays of other dates and logging to add new holidays.

Download the code from Github

The tools and technologies used to create the demo app are:
 * Python 3, the Flask framework and Jinja templating engine.
 * JQuery, the Bootstrap framework, the Materialize framework, HTML and CSS.
 * 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 system, 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
Create a folder named  which will contain 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 http://127.0.0.1:5000/ on your browser. You should see "Holidays and observances" displayed.

Step 3: Create the base layout
The app will have four pages: the homepage which lists holidays for a certain date, a search page to search for custom dates an login page to enter login credentials, and an add page to add new holidays.

Each page will have some common elements:
 * A bar at the top which contains a header that shows the title of the page.
 * An add icon which links to the add page.
 * A search icon which links to the search page.
 * For pages other than the homepage, an arrow-back icon which links to the homepage.
 * Ocassionally, there will be an alert bar to provide information or warnings.

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 in 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.

Note that throughout the application, we'll be using  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:

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

Then call the function in :

After we have the date, we may 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 to get the content of the holidays in that section using API:Parse as well:

Then call the functions in :

The html of the holidays returned contains internal links that point to those holidays on Wikipedia, e.g "/wiki/New_Years_Day". We'll need to prepend "//en.wikipedia.org" to these links using jQuery in order to make them external links, and make the links open in a new tab. To do that create a file called  and add the following code to it:

Then add jQuery to  using:

Step 5: Search for holidays of other dates
To get holidays of custom dates, we need to create a search route which will display a page with a form that collects the month and day to search for:

Once the search form has been submitted, we'll need to update the date whose holidays to fetch to be the date that has been entered. To do that, add the following code to :

Step 6: Log in
The page to which we'll be adding a new holiday has been protected so that anonymous users cannot edit it so we need to login in order to add a new holiday.

Create a new global boolean variable to track the login state of the user:.

To log in, we need to create a login route which will display a page with a form that collects the username and password, and then send a login request with API:Login with the credentials obtained. Once the user logs in successfully,  is set to.

Step 7: Add a new holiday
To add a new holiday, we need to create an add route which checks if the user is logged in. If not, then they will be redirected to the login page. If they are logged in, the add page will contain a form that collects the date and description of the holiday to add, then send an edit request with API:Edit.

The edit adds new holidays to this page on Test Wikipedia, Sandbox/Holidays_and_observances. This is to prevent editing a real page.

The user will then be redirected to the "List holidays" page where the date of holidays fetched is the date they just added holidays to. The holidays added to the test page are formatted in bold to differentiate them from the real holidays.

To fetch holidays from the test page alongside those on the real page, add the following lines of code to :

We also need to update html to contain the test holidays as well so change the  to.

Step 8: Styling the app
To add style to our app, create a stylesheet named  and link to the stylesheet 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.