0

I'm developing a stock market app with a python flask framework. The user has a route where he can see his details and the number of shares.

If a change in the user shares has occurred, I would like it to be displayed automatically. (Without the need for the user to refresh the page).

To make a long story short, I would like the '/user_page/<user_name>' route to refresh automatically when the '/update_page/' route is writing to the DB.

@app.route('/user_page/<user_name>', methods=['GET', 'POST'])
def user_page(user_name):
    user = Users.query.filter_by(user_name=user_name).first()
    return render_template('user_page.html', user=user)

@app.route('/update_page/<user_name>', methods=['GET', 'POST'])
def update_user_holdiongs(user_name):
    if user_name.bid > last_bid:
        change = TradeBids.query.filter_by(id=user_name.id).update(dict(share_amount=user_name.share_amount - bid.share_amount))

db.session.add(bid)
db.session.commit()

3
  • You could use AJAX to achieve this. What do you use for your frontend ? Commented Jun 26, 2021 at 11:58
  • Just a plain HTML no framework Commented Jun 26, 2021 at 12:22
  • You should use Javascript to achieve this. Commented Jun 26, 2021 at 12:27

1 Answer 1

1

You can use AJAX to monitor changes on your backend and refresh (or rewrite) page.
You need something like that:

setInterval(function() {
    // AJAX send request
    $.ajax({
        url: "your url here",
        type: "GET",
        dataType: "json",
        success: function (data) {
            if (data.success) {
                location.reload();
            }
        },
        error: function (request, error) {}
    });
}, 30000);  // check about changes every 30 seconds
Sign up to request clarification or add additional context in comments.

1 Comment

@Monty_emma have a nice day) You can mark answer as correct)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.