Advertisement
Guest User

Untitled

a guest
Apr 10th, 2015
364
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.24 KB | None | 0 0
  1. import numpy as np
  2. from bokeh.plotting import figure, output_file, show, VBox
  3. from bokeh.models import Plot, Range1d, ColumnDataSource
  4. from bokeh.properties import Instance
  5. from bokeh.server.app import bokeh_app
  6. from bokeh.server.utils.plugins import object_page
  7. from bokeh.models.widgets import HBox, Slider, TextInput, VBoxForm
  8.  
  9. xr = Range1d(start=-3, end=3)
  10. yr = Range1d(0, 1)
  11.  
  12. class SlidersApp(HBox):
  13. """An example of a browser-based, interactive plot with slider controls."""
  14.  
  15. extra_generated_classes = [["SlidersApp", "SlidersApp", "HBox"]]
  16.  
  17. inputs = Instance(VBoxForm)
  18.  
  19. text = Instance(TextInput)
  20.  
  21. a = Instance(Slider)
  22.  
  23. p1 = Instance(Plot)
  24. source = Instance(ColumnDataSource)
  25.  
  26. @classmethod
  27. def create(cls):
  28. """One-time creation of app's objects.
  29.  
  30. This function is called once, and is responsible for
  31. creating all objects (plots, datasources, etc)
  32. """
  33. obj = cls()
  34.  
  35. obj.source = ColumnDataSource(data=dict(x=[], y=[]))
  36.  
  37. obj.text = TextInput(
  38. title="title", name='title', value='cubic function'
  39. )
  40.  
  41. obj.a = Slider(title="a", name='a', value=4, start=0, end=4, step=0.1)
  42.  
  43. # toolset = "crosshair,pan,reset,resize,save,wheel_zoom"
  44. toolset = ''
  45.  
  46. # Generate a figure container
  47. p1 = figure(title_text_font_size="12pt",
  48. plot_height=400,
  49. plot_width=400,
  50. tools=toolset,
  51. title=obj.text.value,
  52. x_range=xr, y_range=yr,
  53. background_fill='#59636C'
  54. )
  55.  
  56. # Plot the line by the x,y values in the source property
  57. p1.line('x', 'y', source=obj.source, line_width=3, color='orange')
  58.  
  59. obj.p1 = p1
  60. obj.update_data()
  61.  
  62. obj.inputs = VBoxForm(
  63. children=[
  64. obj.text, obj.a,
  65. ]
  66. )
  67.  
  68. obj.children.append(obj.inputs)
  69. obj.children.append(VBox(obj.p1,))
  70.  
  71. return obj
  72.  
  73. def setup_events(self):
  74. """Attaches the on_change event to the value property of the widget.
  75.  
  76. The callback is set to the input_change method of this app.
  77. """
  78. super(SlidersApp, self).setup_events()
  79. if not self.text:
  80. return
  81.  
  82. # Text box event registration
  83. self.text.on_change('value', self, 'input_change')
  84.  
  85. # Slider event registration
  86. for w in ['a', ]:
  87. getattr(self, w).on_change('value', self, 'input_change')
  88.  
  89. def input_change(self, obj, attrname, old, new):
  90. """Executes whenever the input form changes.
  91.  
  92. It is responsible for updating the plot, or anything else you want.
  93.  
  94. Args:
  95. obj : the object that changed
  96. attrname : the attr that changed
  97. old : old value of attr
  98. new : new value of attr
  99. """
  100. self.update_data()
  101.  
  102. def update_data(self):
  103. """Called each time that any watched property changes.
  104.  
  105. This updates the sin wave data with the most recent values of the
  106. sliders. This is stored as two numpy arrays in a dict into the app's
  107. data source property.
  108. """
  109. N = 1000
  110.  
  111. # Get the current slider value
  112. a = self.a.value
  113.  
  114. # Generate the wavefunction
  115. x = np.linspace(-3, 3, N)
  116. y = x**3
  117.  
  118. self.source.data = dict(x=x, y=y)
  119.  
  120. self.p1.y_range.start = np.min(y) * 1.2
  121. self.p1.y_range.end = np.max(y) * 1.2
  122.  
  123. px = x[N//2:N//2+a*100]
  124. px = np.append(px, px[::-1])
  125. py = y[N//2:N//2+a*100]
  126. py = np.append(py, np.zeros(len(px//2)))
  127. print('YYYYYY',type(self.p1))
  128. self.p1.patch(px, py, color='orange', fill_alpha=0.4)
  129.  
  130.  
  131. # The following code adds a "/bokeh/sliders/" url to the bokeh-server. This
  132. # URL will render this sine wave sliders app. If you don't want to serve this
  133. # applet from a Bokeh server (for instance if you are embedding in a separate
  134. # Flask application), then just remove this block of code.
  135. @bokeh_app.route("/cubic/")
  136. @object_page("cubic")
  137. def make_cubic():
  138. app = SlidersApp.create()
  139. return app
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement