Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import numpy as np
- from bokeh.plotting import figure, output_file, show, VBox
- from bokeh.models import Plot, Range1d, ColumnDataSource
- from bokeh.properties import Instance
- from bokeh.server.app import bokeh_app
- from bokeh.server.utils.plugins import object_page
- from bokeh.models.widgets import HBox, Slider, TextInput, VBoxForm
- xr = Range1d(start=-3, end=3)
- yr = Range1d(0, 1)
- class SlidersApp(HBox):
- """An example of a browser-based, interactive plot with slider controls."""
- extra_generated_classes = [["SlidersApp", "SlidersApp", "HBox"]]
- inputs = Instance(VBoxForm)
- text = Instance(TextInput)
- a = Instance(Slider)
- p1 = Instance(Plot)
- source = Instance(ColumnDataSource)
- @classmethod
- def create(cls):
- """One-time creation of app's objects.
- This function is called once, and is responsible for
- creating all objects (plots, datasources, etc)
- """
- obj = cls()
- obj.source = ColumnDataSource(data=dict(x=[], y=[]))
- obj.text = TextInput(
- title="title", name='title', value='cubic function'
- )
- obj.a = Slider(title="a", name='a', value=4, start=0, end=4, step=0.1)
- # toolset = "crosshair,pan,reset,resize,save,wheel_zoom"
- toolset = ''
- # Generate a figure container
- p1 = figure(title_text_font_size="12pt",
- plot_height=400,
- plot_width=400,
- tools=toolset,
- title=obj.text.value,
- x_range=xr, y_range=yr,
- background_fill='#59636C'
- )
- # Plot the line by the x,y values in the source property
- p1.line('x', 'y', source=obj.source, line_width=3, color='orange')
- obj.p1 = p1
- obj.update_data()
- obj.inputs = VBoxForm(
- children=[
- obj.text, obj.a,
- ]
- )
- obj.children.append(obj.inputs)
- obj.children.append(VBox(obj.p1,))
- return obj
- def setup_events(self):
- """Attaches the on_change event to the value property of the widget.
- The callback is set to the input_change method of this app.
- """
- super(SlidersApp, self).setup_events()
- if not self.text:
- return
- # Text box event registration
- self.text.on_change('value', self, 'input_change')
- # Slider event registration
- for w in ['a', ]:
- getattr(self, w).on_change('value', self, 'input_change')
- def input_change(self, obj, attrname, old, new):
- """Executes whenever the input form changes.
- It is responsible for updating the plot, or anything else you want.
- Args:
- obj : the object that changed
- attrname : the attr that changed
- old : old value of attr
- new : new value of attr
- """
- self.update_data()
- def update_data(self):
- """Called each time that any watched property changes.
- This updates the sin wave data with the most recent values of the
- sliders. This is stored as two numpy arrays in a dict into the app's
- data source property.
- """
- N = 1000
- # Get the current slider value
- a = self.a.value
- # Generate the wavefunction
- x = np.linspace(-3, 3, N)
- y = x**3
- self.source.data = dict(x=x, y=y)
- self.p1.y_range.start = np.min(y) * 1.2
- self.p1.y_range.end = np.max(y) * 1.2
- px = x[N//2:N//2+a*100]
- px = np.append(px, px[::-1])
- py = y[N//2:N//2+a*100]
- py = np.append(py, np.zeros(len(px//2)))
- print('YYYYYY',type(self.p1))
- self.p1.patch(px, py, color='orange', fill_alpha=0.4)
- # The following code adds a "/bokeh/sliders/" url to the bokeh-server. This
- # URL will render this sine wave sliders app. If you don't want to serve this
- # applet from a Bokeh server (for instance if you are embedding in a separate
- # Flask application), then just remove this block of code.
- @bokeh_app.route("/cubic/")
- @object_page("cubic")
- def make_cubic():
- app = SlidersApp.create()
- return app
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement