As I was checking out the Bokeh package I noticed that the tutorials use explicit import statements like from bokeh.plotting import figure
and from numpy import linspace
. I usually try to avoid these in favor of, e.g., import numpy as np
, import matplotlib.pyplot as plt
. I thought this is considered good practice as it helps to avoid namespace contamination.
Is there any reason why Bokeh deviates from this practice, and/or are there common aliases to use for Bokeh imports (e.g. import bokeh.plotting as bp
)?
Since bokeh
has many different sub-modules from which you can pull functions for your plot that are outside of bokeh.plotting
, there is not one all encompassing package that you can import to access all of the functions. One way to get around this is to create your own sub-package in a separate file which will import all of the functions that you want to have access to in your main file. For instance, you could create a file name my_bokeh.py
in the same directory as your main script, and put in the following imports (for example):
from bokeh.models import Range1d,ResizeTool,HoverTool,ColorBar,LinearColorMapper,BasicTicker,Title,BoxSelectTool
from bokeh.models.widgets import TextInput
from bokeh.layouts import row,widgetbox
from bokeh.embed import components
from bokeh.models.callbacks import CustomJS
from bokeh.plotting import figure, show, output_file
Now, in your main script, you can import this sub-package as:
import my_bokeh as bpt
...
bpt.figure(x_range=bpt.Range1d(0,x-1,bounds='auto'),
y_range=bpt.Range1d(0,y-1,bounds='auto'),
plot_width=200, plot_height=200)
...
p.add_tools(bpt.ResizeTool())
...
bpt.show(p)
You'll have access to each function under the bpt
alias as seen in the script above and you can keep the import statements in your main script clean.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With