Hi all,

Just started using Python a week or so ago, really enjoying it so far, but stuck with a project I'm working on. I'm trying to create a in-dash display system which uses Python to communicate with a piece of hardware and uses an HTML page to display the data Python receives. The frontend GUI needs to be pretty spiffy hence why I thought to use HTML (can then add Javascript, flash, etc). The HTML page will be accessible only via localhost and not on the internet.

For example, say the hardware generates the number '10493', Python then reads this, then somehow the result is displayed on the HTML page. The data generated by the hardware will be continuous at timed intervals, so the webpage needs to refresh whenever new data is received (or on a set time).

So here's where I'm stuck. I've looked into CGI but I'm not sure how to implement an automatic fetch or refresh. I would also like to have only parts of the web page refresh if possible, instead of the entire page. Any ideas how I would go about implementing this? Maybe there's a better suggestion to HTML, like Flash? Any other ideas?

Thanks in advance

Dani AI

Generated

For 's in‑dash use: run a small local Python web server that reads the hardware and exposes the latest value, and use client‑side JavaScript in the browser to update only the DOM elements that change. Three practical update patterns are: simple AJAX polling (easiest), Server‑Sent Events (SSE) for one‑way push, or WebSockets for low‑latency two‑way streams. Polling is a good place to start; SSE/WebSockets scale better if your hardware pushes updates very frequently.

A minimal server pattern (Flask + background reader). Replace the simulated read with your actual hardware call and keep the reader in a separate thread or process so the web server stays responsive.

# server.py
from flask import Flask, jsonify, send_from_directory
from threading import Thread
import time

app = Flask(__name__)
state = {'value': 0}

def read_hardware_loop():
    while True:
        # replace with actual hardware read
        state['value'] += 1
        time.sleep(0.5)

@app.route('/data')
def data():
    return jsonify(state)

@app.route('/')
def index():
    return send_from_directory('static', 'index.html')

if __name__ == '__main__':
    Thread(target=read_hardware_loop, daemon=True).start()
    app.run(host='127.0.0.1', port=5000)

Client side: a tiny fetch loop updates only the element showing the number.

<!-- static/index.html -->
<div id="value">—</div>
<script>
async function poll() {
  try {
    const r = await fetch('/data');
    const j = await r.json();
    document.getElementById('value').textContent = j.value;
  } catch (e) { console.error(e) }
}
setInterval(poll, 500);
poll();
</script>

Notes and quick tips: as pointed toward client‑side JS, use DOM updates for partial refreshes. If your read is blocking, use a thread-safe queue or a separate process and expose only the latest value. For higher rates consider WebSockets (Flask‑SocketIO or an async server) or SSE for simpler one‑way streams. Keep the server bound to 127.0.0.1 for localhost-only access, watch CPU/network load if polling very fast, and add basic error handling/reconnect logic on the client.

Well the problem is that Python and all other CGI scripting languages are server-side scripting languages, which means they are only called when the page is requested for the first time.

From what you say, you will need some sort of client-side scripting language for the web interface. You mentioned Javascript in your post, and that would be a great place to start. You could also use Flash or Java.

I wrote a program a little while ago while was meant to be a desktop application, but I created a web interface to it using some smart CSS/Javascript. The Python does all the processing and the Javascript allows for interaction with the information presented.

Those are my ideas, and if you have any questions feel free to post back.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.