5

Hi I'm trying to get a graph to function with ipywidgets in a Jupyter notebook but am having no such luck. I would like to get the widgets to update based on the dataframe when the update button is functioned. Does anybody know where I am going wrong?

#Import Libraries
import pandas as pd
import folium
import geocoder
import numpy as np
import plotly.plotly as py
from plotly.graph_objs import *
import plotly.offline as py
from ipywidgets import widgets, HBox, Output, interactive

py.offline.init_notebook_mode(connected=False)

#Load Dataset
df = pd.read_excel('LOAS_Biography data (feb 2018).xlsx', sheetname='concerts')

#Rename Columns In DataFrame
df.columns = ['Date', 'City', 'Country', 'Venue', 'Capacity', 'Ticket', 'Tour', 'Event', 'Sold Out', 'Champion']

#Summarises Data by Summing Data Per Country
df = df.groupby('Country', as_index= False).sum()

df = df.head(10)

data = [Bar(x=df.Country,
            y=df.Ticket)]

#Make lists
list1 = list(df['Country'].unique())
#list2 = list(df['City'].unique())

#Create Dropdown Box Widget
w = widgets.Dropdown(
    options= list1,
    value= 'Australia',
    description='Country:',
    disabled=False,
)

#Create Text Box Widget
w1 = widgets.Text(
     value='',
     placeholder='Type something',
     description='Search by:',
     disabled=False
)

#Create Update Button For Widgets
w2 = widgets.Button(description="Update")

def update():
    display(HBox([w, w1, w2]))
    display(py.offline.iplot(data, filename='jupyter-basic_bar'))

def on_button_clicked(b):
    venue = (w.options, w1.value)
    clear_output()
    #print(venue)
    update()

w2.on_click(on_button_clicked)
update()

Thank you

kangaroo_cliff
  • 6,067
  • 3
  • 29
  • 42
Ryan
  • 109
  • 2
  • 10
  • Can you tell us what is not working yet with your code? – Nico Haase May 11 '18 at 08:35
  • When running the code, I can select a venue from the dropdown and type in free text in the search widget but when pressing update it doesn't change anything on the graph? – Ryan May 11 '18 at 08:40
  • As far as I can tell, nothing in the code links the selected country to the data that is plotted. What do you want it to do, and how do envision it being linked? – Vidar May 25 '18 at 09:14

1 Answers1

5

There is no action in the Update button function. I've added few lines to refresh the dataframe before it call the update function. I'm able to filter the graph after pressing the update update with below code

def on_button_clicked(b):
venue = (w.options, w1.value)    
print(w.value)  
clear_output()  

if w.value=='All':
    df1=df
else:
    df1=df[df['CREATED_YYYYMMDD']==w.value]

update(df1)
user10120920
  • 64
  • 1
  • 3