Python Inside HTML behaves much like Microsoft's Active Server Pages, Sun's Java Server Pages and PHP : it's basically a HTML document, in which you insert portions of code written in a programming language - here Python
The mission of the Python Software Foundation is to promote, protect, and advance the Python programming language, and to support and facilitate the growth of a diverse and international community of Python programmers. Become a Member Donate to the PSF.
- Currently I'm having some python files which connects to sqlite database for user inputs and then performs some calculations which sets the output of the program. I'm new to python web programming and I want to know what is the best method to use python on web? Ex: I want to run my python files when the user clicks a button on the web page.
- This guide will explain the process of making web requests in python using Requests package and its various features. Prerequisites Python setup: Download and install the python setup from python.org or you can run python in browser with jupyter notebook.
- ' Online Python Compiler. Code, Compile, Run and Debug python program online. Write your code in this editor and press 'Run' button to execute it.
- Brython Runner requires a hanger server to support Python's input function in the web worker environment. A Brython Runner instance will use the hanger server instance served for the Pythonpad service on default settings. However, you can serve your own hanger server and provide the URL to the server as hangerUrl option.
In Python Inside HTML, these portions of code are separated from the HTML code inside special tags : <% and %>
Suppose you want to display the current date, you'll mix html and Python code this way :
With a text editor, write the code above and save it under time.pih in your Root Directory. Enter http://localhost/time.pih and see what happens
You'll notice that the code inside the <%
and %>
tags is ordinary Python code, in which you can import modules, create and instanciate classes, use variables, read or write to the file system, etc. Access to HTTP environment, to form fields, to the exceptions defined by Karrigell are made the same way as in Python scripts
7.3.1 Python variables
When you only want to print a variable's value, instead of<% print var %>
you can use the shortcut <%= var %>
: In this example you'll notice that you don't have to explicitely import the os
module : for convenience, it is already in the namespace when you execute the script ; so are two other modules, string
and Cookie
, because they will probably be used in many scripts (but of course, if you explicitely write import string
your script will work as well) 7.3.2 Strings for translation
Since internationalization is important in Karrigell, there is a shortcut for the strings or string variables you'll want to be translated according to user preferences : use<%_ string %>
If you have prepared a translation for the string Current directory is
, when the user calls the script and his language preference is French, his browser will display Le répertoire courant est
See Karrigell support for internationalization
7.3.3 Indentation
The result of processing a PIH file is Python code ; this code must be indented. Since a PIH script is a mixture of HTML, in which indentation has no other meaning than readability, and of chunks of Python code, it may be difficult to produce a code that is easily readable and correctly indented
7.3.3.1 Basics
Intallation
PythonInsideHTML follows simple rules :
- at the start of the script, indentation is zero
- every part is indented according to the current indentation
- this current indentation can be modified by two means :
- when a Python code part ends with a colon (:) the indentation of what follows is incremented by 1
- to decrement indentation use
<% end %>
A simple condition example :
and a for
loop :
Without this <% end %>
tag the </table>
tag would have been inserted in the loop
A last one with two levels of indentation
Note that after the 1st line the tag must be closed by %>, if not, the indentation after the second line will be only 1
7.3.3.3 The <indent>
tag
For longer or more complex code the repetitive use of <% end %>
may become tedious. If you want some parts of your code to be indented in Python as it is in the PIH code, embed it with the <indent>
tag
First example :
Second one :
On the line noted (A) above you see that the indentation of the line is relative to the indentation of the <indent>
tag
Also note that after an indented part (after the </indent>
tag) indentation returns to zero
An example with embedded loops :
7.3.4 PIH as a templating system
PIH scripts can be used to create HTML files outside of the Karrigell server, making them a 'templating system'For instance, this HTML documentation is produced from PIH scripts. The chapter number is not fixed but inserted like this :
To produce HTML from a PIH script, use the module Template.py
in folder karrigell/core, create an instance passing it the path of the PIH file , then apply its method render([name_space[,out]])
, passing it the namespace in which it will find the required variables, and a file object with a write()
method (defaults to sys.stdout
)
7.3.5 Debugging
Python Html Test Runner
For debugging purposes start thePIHapp.py