Py4js
a fast and simple micro-framework for small web applications
Install / Use
/learn @lixk/Py4jsREADME
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.
Related Skills
node-connect
343.1kDiagnose OpenClaw node connection and pairing failures for Android, iOS, and macOS companion apps
frontend-design
90.0kCreate distinctive, production-grade frontend interfaces with high design quality. Use this skill when the user asks to build web components, pages, or applications. Generates creative, polished code that avoids generic AI aesthetics.
openai-whisper-api
343.1kTranscribe audio via OpenAI Audio Transcriptions API (Whisper).
qqbot-media
343.1kQQBot 富媒体收发能力。使用 <qqmedia> 标签,系统根据文件扩展名自动识别类型(图片/语音/视频/文件)。
