API:Picture of the day viewer/ar

نظرة عامة
سوف تتعلم في هذا التدريب التعليمي كيفية تصميم عارض ويكيبيديا:صورة اليوم المختارة مستخدما واجهة برمجة تطبيقات ميدياويكي من النوع Action. تنزيل الكود المصدري من غت هب ابحث عن التطبيق في مصنع الأدوات

سوف يعلمك هذا التدريب التعليمي كيفية تنفيذ هذا مستخدما:


 * بايثون 3 وكذا إطار عمل فلاسك
 * واجهة برمجة تطبيقات ميدياويكي التي تحمل اسم Action وكذا وحدات و البرمجية

تأسيس بايثون
This tutorial uses Python 3. You can download the latest Python version from here:


 * Python for Windows 7, 8, and 10
 * Python for Mac OS X

See the Python beginner's guide for further instructions on installing Python on different operating systems.

تأسيس فلاسك
Pip is a package manager that you can use to install Flask:. If you don't have pip already, install it from the official Pip website.

الخطوة 2: إنشاء تطبيق فلاسك بسيط
If you have everything successfully installed, place the following script in, inside your project folder:. When you run it, it should display "Hello world" at http://localhost:5000/:

الخطوة 3: عارض صورة اليوم المختارة
Now that you have everything set up, you can start writing code for the Picture of the Day viewer. Picture of the day, or POTD, is a featured image displayed daily on the home page of Wikipedia. You'll be getting the picture from a wiki template that changes every day.

جلب تاريخ اليوم
The first order of business is simply knowing what day it is. Because POTD updates daily, you need today's date to access the archives and get at a stable version of the correct picture. To do this, import Python's class.

Next, define a new function,. will render the web page and pass along any data associated with our API calls. See for more information on the   file we'll be using as a template. For now,  should contain a variable holding the current date. We'll be using it soon to compose a query to access the POTD.

استدعاء واجهة برمجة التطبيقات التي تحمل اسم Action
The Action API works by sending back data in response to HTTP requests. To do so, you will import the Python Requests library.

Next add two new variables:  and. You will use the  object to make requests to the   url.

In a new function,, call API:Images to request the picture embedded within a protected POTD page (example). From this call, use the picture's filename to call API:Imageinfo, and retrieve the image's source url. In this example, the second API call is handled in the helper function,.

Wikipedia's archives lists dates by ISO standard format -- for instance, 2019-01-31, for 31 January, 2019. You can get the correct format by using the date method,.

Finally, alter  to call. Import  from , and make   return.

عرض الصفحة


Flask templates mostly contain HTML markup, but they also use Jinja to render dynamic content. Jinja markup looks like this --  -- and is used to inject Python variables or expressions into our basic page structure. Add some basic HTML 5 boilerplate and a few elements to. Make sure to save it to a directory within your app, named.

تحويلها إلى صفحة تفاعلية
Add a element to , and give it the following submit button inputs: Back and Next. When either button is selected, the form will submit a POST request, and the selected value will be passed back to. This will allow users to browse through the Picture of the Day archives.

Next, update  with a   function, for setting the date presented to the user. Also extend the  route to handle POST requests from the form. To allow  to read the POST request message, import Flask's   class.

The complete Python and HTML code:

Styling your app
Flask uses a directory, named static, to contain any helper files that stay the same throughout the lifecycle of the app. This is a useful place to put any stylesheets or additional scripts. Our stylesheet will be using some colors and visual motifs based on the Wikimedia Style Guide. Place a CSS file in.

الخطوات التالية

 * Got questions about this tutorial? Ask here on Discourse.
 * Contribute a demo app that you have developed using the MediaWiki API to this code samples repository.
 * Learn some ways to add descriptive text from the Picture of the day page:
 * API:Search provides  which can be used as a link preview
 * can be used to obtain the whole parsed text from articles on Wikis that have the Cirrussearch extension installed (see example).

انظر أيضا

 * API:Main page: Overview of the Action API
 * API:FAQ: FAQ describing the Action API in more depth
 * API:Images
 * API:Imageinfo
 * MediaWiki Action API Code Samples repo: Contains more demos and sample code
 * More on Wikipedia and IFTTT