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.htmlin 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
waitresspackage, please install it first bypip install waitressorpip3 install waitress. -
gevent
from gevent import monkey from py4js import Server monkey.patch_all() Server(server='gevent').run()
If you haven't installed the
geventpackage, please install it first bypip install geventorpip3 install gevent.
Asgeventis Asynchronous, the server can be very fast, can handle a virtually unlimited number of concurrent connections and are easy to manage.
Metadata
Release files for py4js 0.1.2
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | Provenance | Actions |
|---|---|---|---|---|
| py4js-0.1.2.tar.gz | 5.3 kB | Without provenance | Details for py4js-0.1.2.tar.gz |
Release files / py4js-0.1.2.tar.gz
| Download URL | py4js-0.1.2.tar.gz |
|---|---|
| Size | 5.3 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
53a589a2d8d60cc3c89a4421057ded2c7449f8c0b763b0ae9bcc0308dfcbcea8
|
|
BLAKE2b-256 checksum How to use checksums |
568b25a3dcd9b819ea8b0aaaba45f7346a175656c25642912fdbe2e9cb343cef
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is 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
|
Security for py4js 0.1.2
Provenance
Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.
No provenance found
None of the files in this release were uploaded with provenance metadata.
A note on package security
PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.
To learn more about protecting yourself from malicious actors, visit our package security documentation.