Webフレームワーク

□未翻訳

□翻訳中

□翻訳完了(細田謙二)

■レビュー(Omi Chiba)

Webフレームワーク

最も基本的なレベルでは、Webアプリケーションは、対応するURLが訪問されたときに実行されるプログラム(または関数)の集合からなります。プログラムの出力は訪問者に返され、ブラウザでレンダリングされます。

At its most fundamental level, a web application consists of a set of programs (or functions) that are executed when the corresponding URL is visited. The output of the program is returned to the visitor and rendered by the browser.

Webフレームワークの目的は、新しいアプリケーションを、迅速かつ、容易に、ミスなく構築できるようにすることです。これは、APIやツールを提供することによって行われます。それにより、必要とされるコードの量が減り、単純化されます。

The purpose of web frameworks is to allow developers to build new app quickly, easily and without mistakes. This is done by providing APIs and tools that reduce and simplify the amount of coding that is required.

Webアプリケーションの開発には2つの古典的なアプローチがあります:

The two classic approaches for developing web applications are:

  • HTMLをプログラムで生成する

  • Generating HTML programmatically.

  • HTMLページにコードを埋め込む

  • Embedding code into HTML pages.

第1のモデルは、初期のCGIスクリプトなどが従うものです。第2のモデルは、PHP(コードはC言語に似たPHP)、ASP(コードはVisual Basic)、JSP(コードはJava)などが従うものです。

The first model is the one that was followed, for example, by early CGI scripts. The second model is followed, for example, by PHP16 (where the code is in PHP, a C-like language), ASP (where the code is in Visual Basic), and JSP (where the code is in Java).

ここでは、実行時にデータベースからデータを取得し、選択したレコードを表示するHTMLページを返すPHPプログラムの例を示します:

Here is an example of a PHP program that, when executed, retrieves data from a database and returns an HTML page showing the selected records:

1.

2.

3.

4.

5.

6.

7.

8.

9.

10.

11.

12.

13.

14.

<html><body><h1>Records</h1> mysql_connect(localhost,username,password);

@mysql_select_db(database) or die( "Unable to select database");

$query="SELECT * FROM contacts";

$result=mysql_query($query);

mysql_close();

$i=0;

while ($i < mysql_numrows($result)) {

$name=mysql_result($result,$i,"name");

$phone=mysql_result($result,$i,"phone");

echo "<b>$name</b><br>Phone:$phone<br /><br /><hr /><br />";

$i++;

}

?></body></html>

このアプローチの問題は、HTMLにコードが埋め込まれる際、非常に同じようなコードが、追記するHTMLを生成するのに、また、データベースに問い合わせるSQL文を生成するのに必要となることです。それにより、アプリケーションの複数のレイヤーが絡み合って可読性や保守性が困難になります。この状況はAjaxアプリケーションにおいてより悪化します。さらに、アプリケーションを構成するページ(ファイル)の数とともに複雑さが増加します。

The problem with this approach is that code is embedded into HTML, but the very same code also needs to generate additional HTML and to generate SQL statements to query the database, entangling multiple layers of the application and making it difficult to read and maintain. The situation is even worse for Ajax applications, and the complexity grows with the number of pages (files) that make up the application.

上記の例の機能は、web2pyを使うと2行のPythonコードで表現できます:

The functionality of the above example can be expressed in web2py with two lines of Python code:

1.

2.

def index():

return HTML(BODY(H1('Records'), db().select(db.contacts.ALL)))

この単純な例では、HTMLページの構造が、HTML、BODY、H1オブジェクトを用いてプログラム的に表現されています。データベース db は select コマンドによって問い合わせられ、最終的にすべてHTMLへと加工されます。なお、db はキーワードではなく、ユーザー定義変数です。本書では、曖昧を避けるため、この名前をデータベースコネクションとして一貫して参照します。

In this simple example, the HTML page structure is represented programmatically by the HTML, BODY, and H1 objects; the database db is queried by the select command; finally, everything is serialized into HTML. Notice that db is not a keyword but a user defined variable. We will use this name consistently to refer to a database connection to avoid confusion.

Webフレームワークは典型的に2つのタイプに分類されます:1つは、いくつかのサードパーティのコンポーネントを組み立てて(接合して)構築された「グルー(接着)」フレームワークです。もう一つは、緊密に組み合わせされて協調して動作するように特別に設計されたコンポーネントを組み合わせて構築された「フルスタック」フレームワークです。

Web frameworks are typically categorized as one of two types: A "glued" framework is built by assembling (gluing together) several third-party components. A "full-stack" framework is built by creating components designed specifically to be tightly integrated and work together.

web2pyはフルスタックフレームワークです。ほぼすべてのコンポーネントは、スクラッチで構築され一体となって動作するように設計されています。ただし、それらはweb2pyフレームワーク全体の外でも同様に機能します。たとえば、データベース抽象化レイヤ(DAL)や言語テンプレートは、web2pyフレームワークと独立に使用することができます。これは、独自のPythonアプリケーションにgluon.sqlやgluon.templateをインポートする事によって可能です。gluon はweb2pyのモジュールの名前で、システムライブラリを含んでいます。いくつかのweb2pyのライブラリは、たとえばデータベースのテーブルからフォームを構築し処理する機能などは、別のweb2pyの一部に依存しています。web2pyはまた、他のテンプレート言語やDALなどのサードパーティー製のPythonのライブラリとともに動作させることも可能です。しかし、それらはオリジナルのコンポーネントほどには緊密に連携することはないでしょう。

web2py is a full-stack framework. Almost all of its components are built from scratch and are designed to work together, but they function just as well outside of the complete web2py framework. For example, the Database Abstraction Layer (DAL) or the template language can be used independently of the web2py framework by importing gluon.sql or gluon.templateinto your own Python applications. gluon is the name of the web2py module that contains system libraries. Some web2py libraries, such as building and processing forms from database tables, have dependencies on other portions of web2py. web2py can also work with third-party Python libraries, including other template languages and DALs, but they will not be as tightly integrated as the original components.