挨拶しよう

□未翻訳

□翻訳中

□翻訳完了(細田謙二)

■レビュー(中垣健志)

挨拶しよう

ここでは例として、ユーザーに"Hello from MyApp"というメッセージを表示する簡単なWebアプリケーションを作成します。このアプリケーションを"myapp"と呼びます。また、同じユーザがページを何回訪問したかをカウントするカウンタを追加します。

Here, as an example, we create a simple web app that displays the message "Hello from MyApp" to the user. We will call this application "myapp". We will also add a counter that counts how many times the same user visits the page.

新しいアプリケーションは、adminの中のsiteページの右上にあるフォームに、アプリケーション名を入れることで簡単に作成できます。

You can create a new application simply by typing its name in the form on the top right of thesite page in admin.

[create]ボタンを押すと、アプリケーションは組み込みのwelcomeアプリケーションのコピーとして作成されます。

After you press [create], the application is created as a copy of the built-in welcome application.

新しいアプリケーションを実行するには、次のURLを開いてください:

To run the new application, visit:

http://127.0.0.1:8000/myapp

これで、welcomeアプリケーションのコピーが作成できました。

Now you have a copy of the welcome application.

アプリケーションを編集するには、新しく作成されたアプリケーションのeditボタンをクリックしてください。

To edit an application, click on the edit button for the newly created application.

EDITページは、アプリケーションの内部がどのようなものかを示しています。すべてのweb2pyアプリケーションは一定のファイルから構成され、それらのほとんどは次の5つのカテゴリに分類されます:

The EDIT page tells you what is inside the application. Every web2py application consists of certain files, most of which fall into one of five categories:

  • models: データ表現を記述します。

    • describe the data representation.

  • controllers: アプリケーションのロジックとワークフローを記述します。

    • describe the application logic and workflow.

  • views: データの表示方法を記述します。

    • describe the data presentation.

  • languages: アプリケーションで表示される内容を、他の言語に翻訳するための方法を記述します。

    • describe how to translate the application presentation to other languages.

  • modules: アプリケーションに属するPythonモジュールです。

    • Python modules that belong to the application.

  • static files: 静的画像ファイル、CSSファイル、JavaScriptファイル、などです。

    • static images, CSS files , JavaScript files , etc.

すべてのファイルは、モデル - ビュー - コントローラのデザインパターンに沿ってきちんと構成されます。editページの各セクションは、アプリケーションフォルダ内のサブフォルダに対応します。

Everything is neatly organized following the Model-View-Controller design pattern. Each section in the edit page corresponds to a subfolder in the application folder.

なお、セクションの見出をクリックすると、その中身の表示/非表示を切り替えることができます。同様にstatic filesの下のフォルダ名も、折りたたむことができます。

Notice that section headings will toggle their content. Folder names under static files are also collapsible.

セクション内の各ファイルは、サブフォルダにある物理ファイルに対応しています。管理インターフェイスからファイルに対して行えるすべての操作(create、edit、delete)は、好みのエディタを使用してシェルから実行することもできます。

Each file listed in the section corresponds to a file physically located in the subfolder. Any operation performed on a file via the admin interface (create, edit, delete) can be performed directly from the shell using your favorite editor.

アプリケーションは上記以外にもデータベース、セッションファイル、エラーファイルを含みますが、editページには掲載されません。これらのファイルは管理者ではなく、アプリケーション自身によって作成・編集されるためです。

The application contains other types of files (database, session files, error files, etc.), but they are not listed on the edit page because they are not created or modified by the administrator; they are created and modified by the application itself.

コントローラは、アプリケーションのロジックやワークフローを保持します。すべての各URLは、コントローラ(アクション)のいずれか1つの関数の呼び出しにマッピングされます。"appadmin.py"と"default.py"という2つのデフォルトコントローラが用意されています。appadminは、データベース管理用のインターフェイスを提供しますが、ここでは必要ありません。"default.py"は、編集するべきファイルであり、URLに対応するコントローラが存在しない時にデフォルトで呼び出されます。次のように"index"関数を編集してみましょう:

The controllers contain the logic and workflow of the application. Every URL gets mapped into a call to one of the functions in the controllers (actions). There are two default controllers: "appadmin.py" and "default.py". appadmin provides the database administrative interface; we do not need it now. "default.py" is the controller that you need to edit, the one that is called by default when no controller is specified in the URL. Edit the "index" function as follows:

1.

2.

def index():

return "Hello from MyApp"

オンラインのエディタは次のような表示になります:

Here is what the online editor looks like:

それを保存し、editページに戻ってください。そして、indexのリンクをクリックし、新しく作成されたページを表示してください。

Save it and go back to the edit page. Click on the index link to visit the newly created page.

次のURLを開くと、

When you visit the URL

http://127.0.0.1:8000/myapp/default/index

myappアプリケーションのdefaultコントローラにあるindexアクションが呼び出されます。このメソッドは、ブラウザに表示される文字列を返します。下記のように表示されます:

the index action in the default controller of the myapp application is called. It returns a string that the browser displays for us. It should look like this:

それでは、"index"関数を次のように編集しましょう:

Now, edit the "index" function as follows:

1.

2.

def index():

return dict(message="Hello from MyApp")

また、editページから、default/indexビュー(アクションに関連付けられた新しいファイル)を編集し、このファイルに以下を書き込んでください:

Also from the edit page, edit the view "default/index.html" (the view file associated with the action) and completely replace the existing contents of that file with the following:

1.

2.

3.

4.

5.

6.

<html>

<head></head>

<body>

<h1>{{=message}}</h1>

</body>

</html>

すると、アクションはmessageが定義された辞書を返すようになります。アクションが辞書を返すとき、web2pyは下記の名前を持つビューを探します。

Now the action returns a dictionary defining a message. When an action returns a dictionary, web2py looks for a view with the name

1.

[controller]/[function].[extension]

そして、それを実行します。ここでは[extension]は、リクエストされた拡張子です。拡張子が指定されていない場合は、デフォルトは"html"で、ここではそのように想定しています。この場合、ビューは、Pythonのコードを特別な {{ }}タグを用いて埋め込んだHTMLファイルとなります。この例では特に、{{=message}}の部分が、そのタグ付きのコードを、アクションから返されたmessageの値に置き換えるようにweb2pyに指示します。ただしここで、messageはweb2pyのキーワードではなく、アクションで定義されたものです。ここまで、web2pyのキーワードは使用されていません。

and executes it. Here [extension] is the requested extension. If no extension is specified, it defaults to "html", and that is what we will assume here. Under this assumption, the view is an HTML file that embeds Python code using special {{ }} tags. In particular, in the example, the{{=message}} instructs web2py to replace the tagged code with the value of the messagereturned by the action. Notice that message here is not a web2py keyword but is defined in the action. So far we have not used any web2py keywords.

もしweb2pyがリクエストされたビューを見つけられなかった場合、すべてのアプリケーションで用意されている"generic.html"が使われます。

If web2py does not find the requested view, it uses the "generic.html" view that comes with every application.

もし拡張子が"html"以外のもの(たとえば"json")が指定されて、かつ"[controller]/[function].json"というビューファイルが見つからなかった場合、web2pyは"generic.json"というビューを探します。web2pyでは、generic.html、generic.json、generic.xml、generic.rssというファイルが用意されています。これらの汎用的なビューは、アプリケーションごとに個別に変更することができます。そして新しいビューも簡単に追加できます。

If an extension other than "html" is specified ("json" for example), and the view file "[controller]/[function].json" is not found, web2py looks for the view "generic.json". web2py comes with generic.html, generic.json, generic.xml, and generic.rss. These generic views can be modified for each application individually, and additional views can be added easily.

このトピックに関する詳細は、第9章を読んでください。

Read more on this topic in Chapter 9.

"EDIT"ページに戻りindexをクリックすると、次のHTMLページが表示されます:

If you go back to "EDIT" and click on index, you will now see the following HTML page: