Syntax and features for writing python code in html ----- webpy-based http Server

Source: Internet
Author: User
Tags builtin


1: variables in the python program are passed in to html using the following method::

1: using global variables: global variables do not need to be passed using the $ def with syntax, as long as they are defined
It can be used in html, for example:
========================================================== ======================================
# Template public variables. The following can define the variables used by all html files, without complex
$ Def with (va, vb)
T_globals = {
'Datestr': web. datestr,
'Cookies': web. cookies,
"Loginform": login,
"Gposts": model. get_posts,
# Specify the template directory and set the public template. base = "base" sets the commonly used base.html template,
Find the file in the path./templates/base.html.
Render = web. template. render ('templates', base = 'base', globals = t_globals)
========================================================== ========================
2: It is passed in during render in a python program. The example is as follows:
========================================================== ========================
In the python file,
Render = web. template. render ("./")
Class index:
Def GET (self ):
Abc = "world"
Render. index (name = abc)
In the index.html file:
$ Def with (name)
Hello $ name
========================================================== ==============================
The above example shows that the name is passed in to the index () function in the python file,
In the index.html file, you must define a temporary variable to accept the input variable.
Abc is the name of a variable in python.
Name is the name of the variable in the html file,
Variable transfer is implemented in render. index (name = abc,
Note: In python, render. index (a, B) can pass multiple variables.
Then, the corresponding Temporary Variable $ def with (va, vb) should be declared in the html file)
========================================================== ==============================
2: several methods to use the template:
1: directly use the HTML file and input the python variable to the index.html file. The example is as follows,
In python:
class index:
  def GET(self):
	return reder.index("wolrd")
#Templatesis a directory. All HTML files are stored in the templatesdirectory, for example, index.html.
2: specify the specific file directly. This method is not good for expansion,

return hello("world")
3: Use a string
html="$def with (name)\n hello $name"
return hello("wolrd")
========================================================== ======================================
We can see three methods to call the template:
render = web.template.render ("templates") specifies only the path to the html file
render.index ("world")

hello = web.tempalte.frender ("templates / hello.html") specifies the specific html file
hello ("world")

hello = web.template.Template (string) directly pass the string in,
hello ("world")
========================================================== ======================================
The above three methods are most commonly used in the first, render. index method,
========================================================== ======================================
3: The following is the basic syntax of python in html files.
1: Get the value of the variable. Note that it is just a syntax and there are not many reasons.

2: Create a variable in the html file. It must be created only when the value is assigned.

The syntax is as follows. It is important to add a space and a variable name to $.
$ bug=True
$ va=1

3: When getting the value of a variable, you will see two syntaxes:

First: $
Type 2: $:
By default, python uses web. websafe filter to perform HTML-encoding on variables. This is the first method. The second method does not perform html-encoding on variable.

4: \ This symbol is a bit interesting. It will show only one row of content in multiple rows.

Hello \
Note: Press enter immediately after the \ symbol. Otherwise, the special meaning of \ will disappear and be displayed together.

5: ask you a question: how to display the $ symbol in the html file (because it is used specially for webpy)

The answer is simple. Just enter two $ S.
The dollar sign is $
Show only one $ above

6: How to Write python-style comments in html? I'm not talking about it. <! Such a comment>

$ # This is a comment, which you cannot see in the browser. webpy gives the comment to the filter.

7: In the control flow section, note that the indentation of the I want sentence must be greater than two spaces,

You can press the tab button normally.
$ for i in range (10):
    i want eat $ i apple (s)

$ a = 4
$ while a <10:
    $ a
    $ a + = 1

$ if a> 10:
    hell $ a
$ else:
    keep on, you will do it

A for example in html application to create a table like this
$ for c in ["a", "b", "c", "d"]:
     <tr class = "abc">
         <td> $ index </ td>
         <td> $ c </ td>
     </ tr>
</ table>

8: some other useful things, such as $ def

You can also define functions in html. How convenient this is.
$def tr(value):
	$for i in value:

$def table(rows):
	$for row in rows:

$ data=[['a', 'b', 'c'], [1, 2, 3], [2, 4, 6], [3, 6, 9] ]

$:table([tr(d) for d in data])

9: there is also a magic keyword code. All python code can be written under the code block:

    x = "you can write any python code here"
    y = x.title()
    z = len(x + y)

    def limit(s, width=10):
        """limits a string to the given width"""
        if len(s) >= width:
            return s[:width] + "..."
            return s
Back to html
The variables defined above can also be used here,
For example
$ Limit (x)

10: var block, which is difficult to understand. See the following code.
In html
$def with (title, body)

$var title: $title
$var content_type: text/html

<div id="body">
In python
>>> out ='hello', 'hello world')
>>> out.title
>>> out.content_type
>>> str(out)
'\n\n<div>\nhello world\n</div>\n'
We can see that the var keyword is used to pass the variables defined in html back to the python program,
Python can perform more processing based on the content,

11: builtin functions and variables that can be accessed in html files. Common functions are

Accessible, such as max, min, range,
True, False is also recognizable,
A concept corresponding to builtin is the globals variable or function of a specific application,
How can these globals variables or functions be accessed by all html templates?
import web
import markdown
In this way, you can use the makrdown function in all html files.
This function is like builtin,

12: For security reasons, the following command cannot appear in the html Template

Import, exec
Attribute access cannot start,
Open, getattr, and setattr functions cannot be used.
If your template is accidentally used, the security of SecurityException will occur.
You can write python in html after you know the above,

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.