# Pylatte Official Webpage

Home

# Tutorial

## Hello Pylatte

Let's print Hello Pylatteon pylatte tutorial as a first step. First of all, open a text editor, and it will be saved as hellopy.pyl

You can use python code in pyl file format on pylatte. pyl consists of HTML and python.

To make pyl page working on pylatte, wrap python code with <$ and $>. And if you put your variable between <$= and $> such as <$=variable$>, the variable will be automatically printed on the web page by pylatte translation module.

Caution : If you use print() function, it will be printed on web server console.

#

    				<$
    				hi="hello pylatte"
    				$>
    				<$=hi$>

In python language, indentation is absolutely important. So we strongly recommend to use python code under <$, and type $> on new line after python code. Refer to the following code.

Type the code in hellopy.pyl and save it again. After running pylatte web server, you can see hello pylatte.

## Starting pylatte web server

Basically, default port number is 80 on HTTP. If you want, you can change port number in pylatte_config.xml

    						80

There are many options in pylatte_config.xml. To change port number, we need to focus on only <pylatte-server> tag. Modify the value of <port> tag from default port(80) to what you want(e.g. 8000).

Caution : You have to check if the port number is being used by another program before you change.

To start pylatte web server, move to appropriate path where pylatte is installed and run following command into terminal. If operating system is Windows, just execute the latteServerStart.py file.

    					sudo python3 latteServerStart.py

There are just one more step to see hello pylatte. Take URL Mapping below.

## URL Mapping

To make pylatte security safe, it is neccesary to fix <pylatte> and <pylatte-mapping> tag in pylatte_config.xml

    						hello_pylatte
    						hellopy.pyl

    						hello_pylatte
    						/py

<pylatte-name> value(e.g. hello_pylatte) in <pylatte> and <pylatte-mapping> is just name to identify each mapping information.

<pylatte-pyl> value(e.g. hellopy.pyl) in <pylatte> is real pyl page we make. it will be accessed by <url-mapping> value in <pylatte-mapping>. Once more, you have to access http://localhost/py, NOT to http://localhost/hellopy.pyl. This can hide original url, so we think it would make pylatte securtiy much higher.

Congratulation, now you can access to http://localhost/py instead of http://localhost/hellopy.pyl to see hello pylatte.

## Filter

Sometimes, we need to process one same function in many pages before the pages get started. Filter module on Pylatte perfectly supports this.

It can be configured in pylatte_config.xml.

    						testFilter
    						filter/filter.pyl

    						testFilter
    						hellopy.pyl
    						byepy.pyl

Look at this xml code. <filter-name> value(e.g. testFilter) in <filter> and <filter-mapping> is just name to identify each filter information.

<filter-pyl> value will be executed at first when you access to <filter-url> values.

For instance, filter/filter.pyl will be executed at first when you access to byepy.pyl or hellopy.pyl in the code.

So you don't need to add same code to many pages which require same function anymore.

## GET & POST

There are two kinds of method to deal with GET & POST parameter.

When you request some parameter values using GET method or POST method, you can get the parameter values as following.

    					<$=param$>

The variable param has all requested values as dictionary type like {"id":pylatte, "password":python}. Definately, you can get specific value such as following.

    					<$=param["id"]$>

Pylatte does not support the other methods like PUT, DELETE.

## Session

Pylatte supports sesson management allocating session datas for each accessor.

This session data type is dictionary. The way to use is similar with GET & POST above. Refer to following code.

    					session["name"] = "pylatte"
    					session["age"] = 1

Pylatte manages session data through session module. Usually, it is required to use in membership page to deal with each member's session data.

## File Upload

You can implement file upload function with a file upload module on Pylatte. When you use <form> tag, it is compulsory to put attribute : enctype="multipart/form-data into the tag.

Now you can upload a file as post parameters in pageToProcessPost(This is a mapped page name by mapping module at pylatte_config.xml) page. Refer to following code.

    					<$
    					pyformFile=formFile.formFile(pyFile)
    					pyformFile.moveUploadFile("filepy","download","pylatte.jpg")
    					$>

you can receive the file information to variable with using formFile.formFile(pyFile). And execute moveUploadFile mothod from the variable to complete upload file to server. Parameters of moveUploadFile are name, folder name, and file name in order.

In addition, pyFile[name]

has a little information about the file as dictionary type.

## Database

First of all, to set your database, it is needed to fix <sql> values in db_mapping.xml about MySQL connection information.

    						localhost
    						user
    						password
    						pylatte

Only MySQL is an available database on Pylatte now. In <sql>, it is required to fill with host, user, password, database name.

### Simple way to use database

<@latteDatabase> has to be written on the code specifically before using database. After that, execute SQL what you need through latteDB.query() method. And then, use latteDB.store_result() method which return result of processing the SQL.

The result include not only result of SQL but also some methods. To see how to use a database, check example code below.

    					<$
    					latteDatabase()
    					latteDB.query("select * from test")
    					result=latteDB.store_result()
    					$>
    					<div id="dbdiv">
    					<$=result.fetch_row()$>
    					</div>

This code returns tuple type result by fetch_row()

fetch_row() method takes some additional parameters.

The first one is, how many rows (maxrows) should be returned. By default, it returns one row. It may return fewer rows than you asked for, but never more. If you set maxrows=0, it returns all rows of the result set. If you ever get an empty tuple back, you ran out of rows.

The second parameter (how) tells it how the row should be represented. By default, it is zero which means, return as a tuple. how=1 means, return it as a dictionary, where the keys are the column names, or table.column if there are two columns with the same name (say, from a join). how=2 means the same as how=1 except that the keys are always table.column.

### Advanced way to use database

<@latteDatabaseExt> has to be written on the code specifically before using database.

In this case, DBMappingParser class and db_mapping.xml file are required to use. Refer to following code.

    						INSERT INTO table VALUES ($name$, $age$)
    						UPDATE table SET age = $age$ WHERE name = $name$
    						DELETE FROM table WHERE age = $age$

This is xml code in db_mapping.xml file. <sqlMap> tag needs attribute 'id'. And its child nodes (e.g. select, insert, update, delete ...) also need attribute 'id'. These 'id' are used to call SQL which is values of the childe nodes. Let's see how to deal with database using these SQLs.

    					<@latteDatabaseExt>
    					<$
    					db = DBMappingParser.pyLatteDBMappingParser()
    					parameter = {"age":1, "name":"pylatte"}

    					result1 = db.queryForList("user.select1")
    					result2 = db.queryForList("user.select2", parameter)
    					$>

First of all, declare on variable and call DBMappingParser.pyLatteDBMappingParser() to save database object to the variable.

Secondly, define dictionary type parameter. This will be replaced matched value in SQL.

At last, call queryForList() method via the variable declared on first step.

queryForList() method takes two parameters.

First one indicates specific SQL in db_mapping.xml by id. For example, 'user.select1' indicates a value in specific tag which has id 'select1' in <sqlMap> tag having id 'user'. So 'user.select1' get replaced to "SELECT * FROM test"

Second parameter is optional. When SQL indicated by first parameter includes variable $keys$, this is mandatory. As you see above example code, second parameter is dictionary type. In the parameter, the each values automatically gets replaced with each matched $keys$ in SQL. So select2 will be traslated from SELECT * FROM test WHERE age = $age$ to SELECT * FROM test WHERE age = 1

queryForList() method returns tuple type as a result.

## Server Information

It is possible to get HTTP header information that you required to Pylatte web server. It is also saved as dictionary type. Refer to following code to see how to use it in pyl.

    					<\$=headerInfo["User-Agent"]$>
    					<\$=headerInfo["Accept-Encoding"]$>

There are planty of header information. Pylatte support most common header information.

### More in this section

- Menu
- About
- Download
- Install
- Tutorial
- Documentation
- Comment
- F A Q
- Contact us