API:Holidays viewer/pt

Sinopse
Este tutorial fala sobre como criar uma aplicação de demonstração que obtém os feriados e observância para uma data indicada da Wikipédia, com uma opção para iniciar a sessão para adicionar novos feriados.

Transferir Código do GitHub Explorar Aplicação em 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.

Passo 1: Configurar o ambiente de desenvolvimento Python e Flask
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.

Passo 2: Criar uma aplicação Flask simples
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.

Passo 3: Criar uma disposição base
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:

Passo 4: Listar feriados
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  :

Atualizar hiperligações de feriados
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:

Passo 5: procurar por feriados de outras datas
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 :

Passo 6: Adicionar um feriado
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 :

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:



Passos Seguintes

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

Consulte também

 * API:Main page- The quick start guide for the MediaWiki Action API.
 * API:Parse- Parses the content of a page and obtain the output.
 * API:Edit- Edits a page.
 * API:Login- Allows logging in to a wiki.