helloPython

A hello world program for the Grainite Python API.

This example app shows you how events are sent to a topic and processed with action handlers, as well as how to send further events to other topics in response. All of the code shown below has already been implemented for you in samples/helloPython. It assumes familiarity with Grainite concepts and is mainly intended to introduce Python developers to the Grainite Python API.

Prerequisites

  • You have followed the instructions to install Python and download the grainite_cl Python module in Environment Setup

If you haven't already, follow the instructions in Environment Setup to install the gx CLI and clone the sample apps Git repository. After doing so, you will be able to access helloPython undersamples/helloPython.

Deploy and run helloPython onto Grainite

Loading and running the application is as easy as running a couple of commands:

cd helloPython

# Load the application on Grainite
# Note: GX is an alias set to samples/gx
gx load

# Send work to the application
python3 src/Client.py

After loading an app with gx load, you can run gx app ls to verify that your app has been loaded. You can also remove any apps currently loaded on the server using gx app remove -a <app name>.

You should see that a few events were sent by the client and processed by the application running in Grainite. Read on to understand how the app works.

Observing helloPython in-action

To see what happened after you sent work to the app, you can look at a few things:

1. App Log

The following will print the application logs:

gx log --follow

You can access the logger using context.get_logger()in your app's source code to print your own log lines (see for example samples/helloPython/src/actions/GreetingHandler.py)

2. Events in a Topic

Run the below command. gx will then ask you to select a topic in the application and will print the events which were sent to that topic.

gx topic dump

3. Grains in a Table

Run the below command. gx will then ask you to select a table in the application and will print the grains in that table.

gx table dump

Understanding the app design

Application Configuration

The YAML file for Python apps in Grainite are largely the same as a YAML file for a Java application. However, we need to specify the language type and the for our action handlers as python and set their script_path to the location of the Python file containing the source code for the action handler. We also need to provide the path to the directory containing the app's source Python files under python_dir:

Python app config
app.py.yaml
app_name: helloPython 
package_id: org.samples
python_dir: src 

tables:
  - table_name: person_table
    key_type: string
    value_type: string 
    action_handlers:
      - name: GreetingHandler
        type: python
        script_path: actions/GreetingHandler.py
        actions:
          - action_name: handleGreeting
            subscriptions:
              - subscription_name: peopleEvents 
                topic_name: people 
                topic_key: user_id

topics:
  - topic_name: people
    key_name: user_id
    key_type: string
    value_type: string

  - topic_name: greetings 
    key_name: user_id
    key_type: string
    value_type: string

Application Logic

The application client has been implemented in samples/helloPython/src/Client.py.

The application has a single action handler, which has been implemented in samples/helloPython/src/actions/GreetingHandler.py.

You may refer to the inline comments these two Python files to understand how the Grainite Python API is used by the application. samples/helloPython/src/Client.py shows how events are sent to a topic by the client in Python, while samples/helloPython/src/actions/GreetingHandler.py shows how the events are processed with action handlers.

Currently, gx gen, which is used to auto-generate the action handler and client source code files given an app YAML file, is only supported for Java apps. The source code files for Python apps, like the two specified above, currently need to be created manually after creating the app YAML file.

Last updated