a fast and simple micro-framework for small web applications
Project description
py4js
py4js is a fast and simple micro-framework for small web applications. Its goal is to enable you to develop web applications in a simple and understandable way.
With it, you don't need to know the HTTP protocol, or how Python communicates with JavaScript. You can use Python functions in JavaScript just like native JavaScript functions.
Usage steps
Step 1
Install py4js package: pip install py4js
or pip3 install py4js
Step 2
First, create a package, named service, and then create a Python file in the package, such as hello.py
:
def say_hello(name):
"""
say hello test
:param name:
:return:
"""
return 'Hello %s!' % name
Step 3
Create a Python file that is at the same level as the service package, such as main.py
:
from py4js import Server
Server().run()
Then run it(By default, the server will launch wsgiref server at port 5000
. You can also use other port and wsgi server).
Step 4
Create a HTML file, anywhere, such as index.html
:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>hello</title>
<script src="http://localhost:5000/service.js"></script>
</head>
<body>
<script>
service.hello.say_hello('World', function(data){
alert(data);
});
</script>
</body>
</html>
Open the index.html
in browser and you will see the alert message:
It's so easy, yes?
Server
Server parameters
The server has several startup parameters that can be specified, for example:
Name | Description |
---|---|
host | Server address to bind to(default: 0.0.0.0 ). Pass 0.0.0.0 to listens on all services including the external one. |
port | Server port to bind to(default: 5000). Values below 1024 require root privileges. if port is None, server will use a random port. |
server | Specify the server adapter to use. For more details: Server adapter. (default: wsgiref , others: paste /waitress /gevent /cherrypy /gunicorn .etc). |
service_package | A package that will be scanned by the server. All modules and public functions in the package will be loaded as service for JavaScript. Default package name is service , also you can change it to another name if you like. |
js_route | the path of JavaScript for browser to load. |
access_control_allow_origin | default: * , all cross domain requests are allowed. |
Server adapter
As the py4js server is based on Bottle, the built-in default server is based on wsgiref WSGIServer. This non-threading HTTP server may become a performance bottleneck when server load increases. So it's better to use a different server that is either multi-threaded or supports asynchronous IO.
Bottle ships with a lot of ready-to-use adapters for the most common WSGI servers, such as:
cherrypy
, paste
, waitress
, gevent
, eventlet
, tornado
, twisted
.etc.
Usage:
-
waitress
from py4js import Server Server(server='waitress').run()
If you haven't installed the
waitress
package, please install it first bypip install waitress
orpip3 install waitress
. -
gevent
from gevent import monkey from py4js import Server monkey.patch_all() Server(server='gevent').run()
If you haven't installed the
gevent
package, please install it first bypip install gevent
orpip3 install gevent
.
Asgevent
is Asynchronous, the server can be very fast, can handle a virtually unlimited number of concurrent connections and are easy to manage.
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
File details
Details for the file py4js-0.1.2.tar.gz
.
File metadata
- Download URL: py4js-0.1.2.tar.gz
- Upload date:
- Size: 5.3 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/1.12.1 pkginfo/1.4.2 requests/2.19.1 setuptools/39.0.1 requests-toolbelt/0.8.0 tqdm/4.28.1 CPython/3.6.6
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 53a589a2d8d60cc3c89a4421057ded2c7449f8c0b763b0ae9bcc0308dfcbcea8 |
|
MD5 | 0b5b6b54f70d2007b4156f966447ecd4 |
|
BLAKE2b-256 | 568b25a3dcd9b819ea8b0aaaba45f7346a175656c25642912fdbe2e9cb343cef |