Introduction to widgets
1. Introduction to widgets
Welcome to the final chapter. In this video, we will introduce widgets, discuss the Bokeh layout function, and cover the Div and Spinner widgets.2. Widgets
So what are widgets? A widget is an element which users can interact with to change a plot. Bokeh groups them into 8 categories, including buttons, groups, and sliders. There are over 60 widgets available! Here we can see the Select widget changing glyph color and size based on values of columns in a dataset! We will cover the Select widget later in the chapter; for now, let's start with the Div element, classed as a markups widget.3. Layout and Div
We will build five subplots displaying performance for each basketball position in our NBA dataset and include a title. We previously used some Bokeh layout functions to build subplots. Now we will use the layout function from bokeh-dot-layouts to display widgets and plots together. To include a title above our subplots, we need to use a Div, which is an HTML tag used to define a section of a web page. Don't worry; we won't be learning HTML; we will only use a Div to contain our title. There's a lot of code, so let's break this down. We first import Div from bokeh-dot-models. We then import layout and gridplot from bokeh-dot-layouts. The layout function allows us to pass objects, such as a widget, or a gridplot, as lists to display per row. We subset our data for each basketball position. To create a title, we call Div and pass a string to the text argument. We loop through a list of our subsets. Notice we include the x_range and y_range arguments when creating the figure, passing the minimum and maximum values, respectively. This results in subplots with consistent axes. We assign the first value from the unique values of our position column to the legend_label, allowing us to identify the position in each subplot. Lastly, we call layout inside of show, passing each object inside square brackets - first the title, so it displays in a row at the top, followed by our gridplot.4. Gridplot with title
We now have one subplot for each position and an overall title through the use of a Div.5. Building a spinner widget
Now let's review the Spinner widget, which allows customization of glyph size. We import it from bokeh-dot-models. We call Spinner, give the widget a title, and use low and high to set the minimum and maximum glyph sizes. We assign zero-point-five to the step argument, enabling a 0-point-5 pixel change in glyph size from one mouse click. The value argument sets the starting glyph size, and width assigns the width of the widget in pixels. We use spinner's dot-js_link method, first passing the name of the Bokeh property that will change in the widget, in this case, value. We then state the attribute for the widget to link to, in this case, our glyphs, and finally the glyph's attribute we have linked to, which is its size. Inside a call of show, we call layout and pass our title as a list, along with another list containing our spinner and figure objects.6. Spin into action
Now we don't need to spend time thinking about the size of our glyphs - we can give our stakeholders the choice!7. The dataset
In this chapter, we'll continue working with a hedge fund to analyze stock performance. They have provided us with a dataset called stocks. Here are the first five rows.8. Let's practice!
Now let's start building widgets!Create Your Free Account
or
By continuing, you accept our Terms of Use, our Privacy Policy and that your data is stored in the USA.