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

3
  • Can you tell us what is not working yet with your code? Commented May 11, 2018 at 8: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? Commented May 11, 2018 at 8: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? Commented May 25, 2018 at 9:14

1 Answer 1

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)
Sign up to request clarification or add additional context in comments.

1 Comment

That's just what I was looking for thank you for your help :)

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.