Unable To Plot Graph From PostgreSQL Query Results In Dash App

Multi tool use
Unable To Plot Graph From PostgreSQL Query Results In Dash App
I am attempting to write a simple code to simply plot a bar graph of some fruit names in the x-axis vs corresponding sales units. The aim of this code is just to understand how to query postgres results from heroku hosted database through a dash app.
Below is the code,
from dash import dash
import dash_core_components as dcc
import dash_html_components as html
import plotly.graph_objs as go
import psycopg2
import os
DATABASE_URL = os.environ['DATABASE_URL']
conn = psycopg2.connect(DATABASE_URL, sslmode='require')
cur = conn.cursor()
cur.execute("SELECT fruits FROM pgrt_table")
fruits1=cur.fetchall()
#print(fruits1)
cur.execute("SELECT sales FROM pgrt_table")
sales1=cur.fetchall()
app = dash.Dash()
app.layout = html.Div(children=[
html.H1(
children='Hello Dash'
),
html.Div(
children='''Dash: A web application framework for Python.'''
),
dcc.Graph(
id='example-graph',
figure=go.Figure(
data=[
go.Bar(
x=fruits1, y=sales1, name='SF'),
#{'x': [1, 2, 3], 'y': [2, 4, 5], 'type': 'bar', 'name': u'Montréal'},
],
#'layout':{
# 'title': 'Dash Data Visualization'
#}
)
)
])
if __name__ == '__main__':
app.run_server(debug=True)
The output is below,
Output to the above code
The corresponding output is just the axes with no bar graphs. The connection with the db is working since printing fruits1 or sales1 gives me the values from the columns in postgres. The only issue is the plotting.
NOTE: This question has been heavily modified since the previous draft was extremely vague without any code to show for.
Hey, Nils I have modified the question to be more specific.
– moksha
Jul 1 at 12:59
x='fruits1', y='sales1' - why are you trying to set strings as data? Try to remove the
'
– Nils
Jul 1 at 13:00
'
Just tried it. No luck, just the numbered axes.
– moksha
Jul 1 at 13:02
What exacly is the output of
print(fruits1)
?– Nils
Jul 1 at 13:02
print(fruits1)
1 Answer
1
Example:
fruits1 = [('apple',), ('banana',),
('mango',), ('pineapple',),
('peach',), ('watermelon',)]
The output of your database cannot be used directly:
xData = [fruit[0] for fruit in fruits1]
# gives ['apple', 'banana', 'mango', 'pineapple', 'peach', 'watermelon']
yData = [sales[0] for sales in sales1]
You have to assign your data to the go.Bar
object:
go.Bar
go.Bar(x=xData, y=yData, name='SF')
Just a quick question, xData = [fruit[0] for fruit in fruits1] does this work as a loop or something to pull and store the individual values in fruits1 into xData ?
– moksha
Jul 1 at 13:33
It is basically a loop in a short form. It iterates over each element in
fruits1
and returns the value with index 0. Its also called List comprehension. You can find more informations for e.g. on blog.teamtreehouse.com/python-single-line-loops– Nils
Jul 1 at 13:56
fruits1
By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.
What is exactly your problem?
– Nils
Jun 29 at 11:23