LessonThis is a lesson restated in new words. Any figure in it is an example from the source, not this desk's record.

A trading dashboard in this lesson is the broker session and the Flask session running side by side. The broker object keeps lists of option deltas and prices as ticks arrive. A POST route reads the latest list, a small template renders it, and the Ajax timer from the previous web lesson swaps that HTML into the page about once a second. The page shows the cache growing.

Why it matters

A console print of deltas is enough to prove the cache, and it is a poor way to watch a book. The lesson's point is that the same lists are ordinary attributes, so a web route can read them. Once a route can read a list, it can read other fields the same way: an option price, an underlying price, an open gain and loss, or a blotter, provided those fields exist on the object. The lesson wires one list and treats the rest as the same pattern.

The idea in plain words

Two earlier projects are combined. One script holds the trading application, the gateway connection, the contract stream, and the lists filled by the option callback. The other holds Flask, Jinja, and the Ajax loop that used to redraw a random number. The random draw is removed. In its place the update route reads a list off the trading object, such as the delta list, and passes that list into the fragment template.

Flask's own server has to share the process with the broker thread. The lesson starts the Flask run method on a thread, then constructs the trading application, which already starts its own run loop on a thread. Both stay alive. The page timer keeps posting. Each post sees a longer list when new deltas have been appended.

How to use it

Move the trading class into a module the Flask script can import. Drop the demo's trailing print loop and the extra application instance from that module, so importing it does not start a second session. In the Flask script, import the module and import threading. Create a thread whose target is the Flask app's run method, and start it. Then construct one trading application.

Edit the POST route that used to assign a random decimal. Assign the template variable from the trading object's delta list instead. Leave the fragment template and the one-second Ajax call in place. Rename the fragment later if you want the markup to say deltas. The lesson's first run still uses the old random-number fragment and simply fills it with the list. With the gateway logged in and the stream requested, open the local page. The lesson reports that new deltas appear in the HTML as the callback appends them and Ajax pulls the list.

Open gain and loss, trades, or another stored field can replace the delta list in that one assignment. The route, the fragment, and the timer stay put.

What usually goes wrong

Starting Flask by calling run directly on the main thread never reaches the line that builds the broker object, or it blocks there. The lesson's thread around run is what lets both objects exist. Importing the trading module in a way that also constructs a trading application at import time can open two gateway sessions. The lesson deletes the stray instance so the Flask script owns the only one.

The route must read the same object the callback writes. A second trading application, created inside the route, has empty lists. The lesson keeps one instance at module level and closes over it from the route.

The Ajax path still expects a fragment, JSON, and POST. Pointing the timer at the home route reloads a full page's worth of HTML into the div. And the list on the page is the whole history the cache has appended, which the lesson accepts. It grows on every change. A page that should show only the last delta needs a last-element read the lesson does not add.

The gateway still has to be up and the API switch on. A perfect Flask route displays an empty list if the stream never starts. The lesson's browser check assumes the paper session is already connected.

Questions people ask

Why run Flask on a thread?

The trading object also needs to run in the same process. Starting the web server on a thread lets the script construct the broker session afterward.

What does the page actually refresh?

The lesson points the old update route at the trading object's delta list, and Ajax replaces the div with that list about once a second.

Can the same page show other fields?

Yes, if the field lives on the trading object. The lesson names option prices, underlying prices, open gain and loss, and trades as the same kind of handoff.