Python Forum
Output ZMQ-content to browser
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Output ZMQ-content to browser
#3
I guess you mean Bottle and not PyBottle.
Quote:But do not I ( unnecessarily ) create a new web-server instance?
Yes it's not good and unnecessarily to create a web-server instance for every update.
There are soultion for this and i would recommend Flask,
that has better support for stuff like this like Flask-SocketIO and jQuery + socket.io on the client side.
Also mention Celery that work well with Flask.

The topic of running background live update/stream tasks is complex.
If new to Python and web-develoment dos not help Wink

Can show a solution that call a Python function on server side every 10-sec.
This is using Ajax to no reload browser,and done with jQuery on client side.
To help keep it clean $.ajaxSetup({ cache: false });
Will prevent all future AJAX requests from being cached.
setTimeout( will execute a script only one time) is better setInterval().

app.py:
from flask import Flask, render_template, jsonify, request
import random

app = Flask(__name__)

@app.route("/")
def test_job():
    return render_template('index.html')

@app.route('/stuff', methods=['GET'])
def stuff():
    '''Here stuff to be send to browser'''
    val_random = random.random()   
    return jsonify(val_random=val_random)

if __name__ == "__main__":
    app.run(debug=True)
index.html:
<!DOCTYPE html>
<html>
<head>
  <meta charset="utf-8" />
  <link rel=stylesheet type=text/css href="{{ url_for('static', filename='css/style.css') }}" />
  <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script>
  <title>Show updated random number</title>
</head>
<body>
  <div class="table">
    <p id="rand_value">Value</p>
    <button>Push to start random update</button>
  </div>
</body>

<script> 
  $.ajaxSetup({ cache: false });
  $(document).ready(function () {
    $("button").click(function update_values() {
      $.getJSON('/stuff', function (dat) {
        $("#rand_value").text(dat['val_random']);
      });
      var interval = setTimeout(update_values, 10000);
    });
  });
</script>

</html>
Reply


Messages In This Thread
Output ZMQ-content to browser - by AarClay - Nov-14-2017, 10:37 AM
RE: Output ZMQ-content to browser - by AarClay - Nov-15-2017, 02:59 PM
RE: Output ZMQ-content to browser - by snippsat - Nov-15-2017, 04:20 PM
RE: Output ZMQ-content to browser - by AarClay - Nov-16-2017, 01:16 PM
RE: Output ZMQ-content to browser - by AarClay - Nov-24-2017, 02:26 PM
RE: Output ZMQ-content to browser - by snippsat - Nov-24-2017, 11:31 PM
RE: Output ZMQ-content to browser - by AarClay - Nov-26-2017, 12:01 AM
RE: Output ZMQ-content to browser - by snippsat - Nov-26-2017, 02:16 AM
RE: Output ZMQ-content to browser - by AarClay - Nov-26-2017, 02:01 PM
RE: Output ZMQ-content to browser - by YogiMike - Jun-02-2021, 05:03 PM

Possibly Related Threads…
Thread Author Replies Views Last Post
  JupyterLab Dictionary Content Output Format Ourkid123uk 0 1,387 Sep-04-2020, 02:18 PM
Last Post: Ourkid123uk

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020