Skip to main content

a fast and simple micro-framework for small web applications

Project description


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

def say_hello(name):
    say hello test

    :param name:
    return 'Hello %s!' % name

Step 3

Create a Python file that is at the same level as the service package, such as

from py4js import Server


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">
    <meta charset="UTF-8">
    <script src="http://localhost:5000/service.js"></script>
        service.hello.say_hello('World', function(data){

Open the index.htmlin browser and you will see the alert message: image

It's so easy, yes?


Server parameters

The server has several startup parameters that can be specified, for example:

Name Description
host Server address to bind to(default: Pass 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:

  1. waitress

    from py4js import Server

    If you haven't installed the waitress package, please install it first by pip install waitress or pip3 install waitress.

  2. gevent

    from gevent import monkey
    from py4js import Server

    If you haven't installed the gevent package, please install it first by pip install gevent or pip3 install gevent.
    As gevent is Asynchronous, the server can be very fast, can handle a virtually unlimited number of concurrent connections and are easy to manage.

Project details

Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

py4js-0.1.2.tar.gz (5.3 kB view hashes)

Uploaded source

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page