✊🏿 Black Lives Matter. Please consider donating to Black Girls Code today.
🐇 Announcing Dash VTK for 3d simulation graphics. Check out the March webinar.

How to continuously keep updating table?

In the backend, I have a code running to keep populating a SQLite3 database, called twitter.db, with all tweets as they are posted real time. In the frontend, I have a textbox where the user can enter any word or phrase, and upon clicking a Submit button, tweets with that word or phrase is displayed. However, the displayed tweets table does not keep updating every time a new tweet is tweeted. How do I make this happen? This is my code for the frontend:

import dash
from dash.dependencies import Output, Input, State
import dash_core_components as dcc
import dash_html_components as html
import plotly
import random
import plotly.graph_objs as go
import requests, json
import pandas as pd
import sqlite3

app = dash.Dash()

app.layout = html.Div(style={'height':'100vh', 'width':'100%', 'height':'100%', 'top':'0px', 'left':'0px'}, 
	children=[
		html.Div([
			dcc.Input(id='input_text', placeholder='Enter term whose tweets you want to see', type='text', style={'width': '20%', 'display': 'inline-block'}),
			html.Button('Submit', id='submit_button'),
		]),

		html.Div(id='recent-tweets-table', style={'width': '100%'})
	])

def generate_table(dataframe, max_rows=100):
	return html.Table(
		# Header
		[html.Tr([html.Th(col) for col in dataframe.columns])] +

		# Body
		[html.Tr([
			html.Td(dataframe.iloc[i][col]) for col in dataframe.columns
		]) for i in range(min(len(dataframe), max_rows))]
	)

@app.callback(
	Output('recent-tweets-table', 'children'),
	[Input('submit_button', 'n_clicks')],
	[State('input_text', 'value')])
def update_output(n_clicks, input_text):
	if n_clicks:
		try:
			conn = sqlite3.connect('twitter.db')
			c = conn.cursor()
			df = pd.read_sql("SELECT * FROM sentiment WHERE tweet LIKE ? ORDER BY unix DESC LIMIT 100", conn, params=('%'+format(input_text)+'%',))

			df.sort_values('unix', inplace=True)

			df['date'] = pd.to_datetime(df['unix'],unit='ms')
			df.set_index('date', inplace=True)
			df.dropna(inplace=True)

		except Exception as e:
			with open('errors.txt','a') as f:
				f.write(str(e))
				f.write('\n')

		df.columns = ['Date', 'Tweet', 'Sentiment']
		df = df[['Date', 'Tweet', 'Sentiment']]
		df['Date'] = pd.to_datetime(df['Date'],unit='ms').apply(lambda x: x.replace(microsecond=0))
		df['Time'] = [d.time() for d in df['Date']]
		df['Date'] = [d.date() for d in df['Date']]
		df = df[['Date', 'Time', 'Tweet', 'Sentiment']]

		return generate_table(df.iloc[::-1])

	else:
  		return dash.no_update

if __name__ == '__main__':
	app.run_server(debug=True)

You can test by entering some common word or phrase that is guaranteed to have tweets coming in every few seconds, like “trump” for example. You can see that new tweets are displayed only when clicking the Submit button again.

Check out the Interval component via https://dash.plot.ly/live-updates