Skip to content
Connect > Client Libraries > Python Client

Python Client in Jupyter

This guide uses the Python client in a Jupyter notebook to load a table into a pandas DataFrame and keep a DataFrame up to date from live updates.

Prerequisites

  • A Jupyter kernel running Python 3.14.
  • A client certificate the server accepts, with client.crt.pem, client.key.pem, and the server's 3forge.pub in the notebook's directory. See Connecting and Securing a Client.

1. Install and connect

This stage installs the package and opens the connections the later stages use.

  1. Install the package from a notebook cell:

    %pip install pyami-3forge
    
  2. Connect in a new cell. The listener collects updates into the rows dictionary:

    from pyami.rt.ami_client import (AmiClient, AmiCluster, AmiDatabaseConfig,
                                     AmiSslOptions, AmiSubscriptionConfig)
    
    keys = AmiSslOptions(trusted_server_pub_file="3forge.pub",
                         client_cert_file="client.crt.pem",
                         client_key_file="client.key.pem")
    sql_config = AmiDatabaseConfig("3280", True, "demo", "demo123")
    sql_config.shards[0].ssl = keys
    center_config = AmiSubscriptionConfig("3270", True)
    center_config.shards[0].ssl = keys
    cluster = AmiCluster("N1", "localhost")
    cluster.database_config = sql_config
    cluster.subscription_config = center_config
    
    rows = {}
    
    class Listener:
        def on_update(self, client, name, update):
            if update.action == 3:
                rows.pop(update.id, None)
            else:
                rows.setdefault(update.id, {}).update(zip(update.field_names, update.row_data))
    
        def on_log(self, client, name, level, message):
            if level >= 3:
                print("error:", name, message)
    
    client = AmiClient()
    client.add_listener(Listener())
    client.add_cluster(cluster, sync=True)
    client.sql("N1", "SELECT 1")
    
  3. Check that the cell shows {'1': array([1], dtype=int32)}. An empty {} means the server refused the certificate.

2. Load a table into a DataFrame

This stage creates a small table and reads it into pandas.

  1. Create a table and query it with the query builder:

    1
    2
    3
    client.sql("N1", "CREATE PUBLIC TABLE Quotes (sym String, px Double)")
    client.sql("N1", 'INSERT INTO Quotes VALUES ("IBM", 183.5), ("AAPL", 201.1)')
    client.select("N1", "*", "Quotes", as_frame=True)
    
  2. Check that the cell shows a DataFrame with 2 rows.

3. Keep a DataFrame of live updates

This stage subscribes to the table and shows the merged updates as a DataFrame.

  1. Subscribe, change a row, and show the collected rows:

    1
    2
    3
    4
    5
    6
    7
    8
    import time
    import pandas as pd
    
    client.subscribe("N1", ["Quotes"], sync=True)
    time.sleep(1)
    client.sql("N1", 'UPDATE Quotes SET px = 999.0 WHERE sym == "IBM"')
    time.sleep(1)
    pd.DataFrame.from_dict(rows, orient="index")
    
  2. Check that the DataFrame shows IBM at 999.0, indexed by row id.

Updates arrive on the client's callback thread, not in the cell, so the listener stores them and a later cell shows them. subscribe returns before every row has arrived; the pause gives them time. Run the last line again at any time to see the current state.

4. Clean up

This stage removes the table and closes the connections.

  1. Drop the table and disconnect:

    client.sql("N1", "DROP TABLE Quotes")
    client.remove_cluster("N1", sync=True)
    
  2. Check that the cell runs without output.

The bundled demos

The package also installs 2 demo packages, demo and failover_demo, each with the modules ami_client_demo, crypto_ticker, crypto_ticker_nb, and multiplex_crypto_ticker. They need panel, which the package does not install: without it, importing a demo fails with ModuleNotFoundError: No module named 'panel'. Install it with %pip install panel.

The demos are Panel apps, for a notebook or panel serve. As shipped, they do not run against a default install:

  • They cannot use a client certificate: create_default_multiplex_stack takes use_ssl but no certificate settings, so with or without TLS the connections are refused and nothing is published.
  • They expect 2 instances: by default, Relay ports 3289 and 4289, and Center ports 3271 and 4271.
  • crypto_ticker fails with current Panel: with Panel 1.10.0, panel serve reports Tabulator.__init__() got an unexpected keyword argument 'style'.
  • ami_client_demo has no notebook: its helpers are written for ami_client_demo.ipynb, which the package does not include.

Result

The notebook holds a DataFrame loaded from Quotes, and a second DataFrame that stays current with live updates.