[파이썬] `bokeh` 플러그인 및 확장 기능

Bokeh logo

Bokeh is a powerful open-source Python library that allows you to create interactive visualizations for the web. It provides a simple and elegant way to build interactive plots, dashboards, and applications using modern web technologies such as HTML, CSS, and JavaScript.

In addition to its core functionality, Bokeh also offers a wide range of plugins and extensions that enhance its capabilities. These plugins serve various purposes and can be used to perform specific tasks or add additional features to Bokeh visualizations.

Bokeh plugins

Plugins in Bokeh are Python packages that extend the functionality of the library. They can be added to your Bokeh projects to enable additional features or provide integration with other libraries or services. Here are some popular Bokeh plugins:

bokeh-widgets

This plugin provides a set of interactive widgets that can be added to Bokeh plots. These widgets allow users to interact with the plot and dynamically update its contents. Examples of widgets include sliders, dropdown menus, buttons, and checkboxes.

from bokeh.plotting import figure, show
from bokeh.models import Slider

# Create a simple plot
p = figure()

# Add a slider widget
slider = Slider(start=0, end=10, step=0.1, value=5, title="Value")

# Update the plot based on the slider value
def update_plot(attr, old, new):
    p.circle([1, 2, 3], [4, 5, new])

slider.on_change('value', update_plot)

# Add the widget and plot to a layout
show(column(slider, p))

bokeh-server

The Bokeh server plugin allows you to deploy Bokeh applications as standalone apps that can be hosted on a server. It provides a way to handle multiple users interacting with the same visualization simultaneously.

from bokeh.plotting import figure
from bokeh.models import ColumnDataSource
from bokeh.io import curdoc

# Create a plot and data source
p = figure()
source = ColumnDataSource(data=dict(x=[], y=[]))

# Update the plot based on user input
def update_data():
    # Fetch new data from a data source or perform calculations
    # Update the source.data dictionary with new values
    source.data = new_data

# Register the update_data function as a periodic callback
curdoc().add_periodic_callback(update_data, 1000)

# Add the plot to the document
curdoc().add_root(p)

Bokeh extensions

Extensions in Bokeh allow you to customize and extend the library’s functionality. They are typically JavaScript libraries that integrate with Bokeh to provide additional capabilities. Some popular Bokeh extensions include:

bokeh-leaflet

This extension allows you to create interactive maps using the Leaflet JavaScript library. It provides a way to visualize geographical data with various layers and markers.

from bokeh.plotting import figure, show
from bokeh.tile_providers import CARTODBPOSITRON
from bokeh.models import GeoJSONDataSource
from bokeh.leaflet import LeafletTileSource, GeoJSON

# Define the map tile source
tile_provider = LeafletTileSource(url=CARTODBPOSITRON)

# Create a GeoJSON data source
geojson = GeoJSONDataSource(geojson=geojson_string)

# Create a figure and add the tile layer and GeoJSON overlay
p = figure(x_range=(-2000000, 6000000), y_range=(-1000000, 7000000),
           x_axis_type="mercator", y_axis_type="mercator")
p.add_tile(tile_provider)
p.add_glyph(geojson)

# Show the plot
show(p)

bokeh-embed

This extension allows you to embed Bokeh plots into static HTML documents. It offers a way to generate standalone HTML files that include all the necessary JavaScript and CSS dependencies.

from bokeh.plotting import figure, output_file, save

# Create a plot
p = figure()

# Add glyphs and customize the plot

# Save the plot to an HTML file
output_file("plot.html")
save(p)

Conclusion

Bokeh’s plugins and extensions greatly expand its capabilities and make it a versatile tool for creating interactive visualizations in Python. Incorporating these plugins and extensions into your projects allows you to leverage additional features and integrate with other libraries seamlessly. Experiment with different plugins and extensions to enhance your Bokeh plots and create impressive interactive data visualizations.