i am trying to redirect to a different page on a button click in dash app but it doesn't work

Viewed 2932
import datetime
import sqlite3
import webbrowser
from time import sleep
import dash
import dash_html_components as html
import dash_core_components as dcc

from flask import Response

app = dash.Dash()
server = app.server

app.layout = html.Div([html.H1('Graphs will be added here'),
                       html.P('click on LOG_VIEW to view logger'),
                       dcc.Link(
                           html.Button('LOG_VIEW'),
                           href='/log_stream')])

latest_sno = 0

def flask_logger():
    """creates logging information"""
    global latest_sno, data
    values = ''
    connection = sqlite3.connect(r'C:\Users\rohit\Desktop\newbase.db')
    s = ''
    while True:
        try:
            data = connection.cursor()
            pt = data.execute(f"Select * FROM cvt WHERE SNO >{latest_sno}")
            values = pt.fetchall()
        except sqlite3.Error as e:
            pass
        finally:
            latest_sno = int(
                data.execute("SELECT SNO FROM cvt ORDER BY SNO DESC LIMIT 1").fetchall().__getitem__(0).__getitem__(0))
        current_time = datetime.datetime.now().strftime('%H:%M:%S')
        y = ['name','Address','age','id']
        if(len(values)!=0):
            for i in values:
                s = s + '\n' + str(dict(zip(y, i))) + '\n'

            current_time = '\n'+current_time+s
            yield current_time.encode()
        s = ''
        sleep(0.24)


@server.route("/log_stream", methods=["GET"])
def stream():
    return Response(flask_logger(), mimetype="text/plain", content_type="text/event-stream")


if __name__ == '__main__':
    app.run_server(debug=True)
    webbrowser.open('http://127.0.0.1:8050/')

I am using dash with python, I want to redirect to the location '\log_stream'. The above is the way I tried to implement, the page doesn't get redirected but the URL changes. where am I going wrong or any suggestion on how to do it. Any help will be appreciated Thanks in advance.

this is the link I followed https://github.com/plotly/dash-core-components/issues/189#issuecomment-383228871

1 Answers

A refresh is needed here.

You can set the refresh property of the Link to True:

dcc.Link(html.Button("LOG_VIEW"), href="/log_stream", refresh=True),

refresh (boolean; default False): Controls whether or not the page will refresh when the link is clicked.

https://dash.plotly.com/dash-core-components/link

Related