11.bokeh.py 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235
  1. import subprocess
  2. import time
  3. from percy import percySnapshot
  4. from selenium.webdriver import Chrome
  5. import pywebio
  6. import util
  7. from pywebio import start_server
  8. from pywebio.output import *
  9. def bkapp(doc):
  10. import yaml
  11. from bokeh.layouts import column
  12. from bokeh.models import ColumnDataSource, Slider
  13. from bokeh.plotting import figure
  14. from bokeh.sampledata.sea_surface_temperature import sea_surface_temperature
  15. from bokeh.themes import Theme
  16. df = sea_surface_temperature.copy()
  17. source = ColumnDataSource(data=df)
  18. plot = figure(x_axis_type='datetime', y_range=(0, 25),
  19. y_axis_label='Temperature (Celsius)',
  20. title="Sea Surface Temperature at 43.18, -70.43")
  21. plot.line('time', 'temperature', source=source)
  22. def callback(attr, old, new):
  23. if new == 0:
  24. data = df
  25. else:
  26. data = df.rolling('{0}D'.format(new)).mean()
  27. source.data = ColumnDataSource.from_df(data)
  28. slider = Slider(start=0, end=30, value=0, step=1, title="Smoothing by N Days")
  29. slider.on_change('value', callback)
  30. doc.add_root(column(slider, plot))
  31. doc.theme = Theme(json=yaml.load("""
  32. attrs:
  33. Figure:
  34. background_fill_color: "#DDDDDD"
  35. outline_line_color: white
  36. toolbar_location: above
  37. height: 500
  38. width: 800
  39. Grid:
  40. grid_line_dash: [6, 4]
  41. grid_line_color: white
  42. """, Loader=yaml.FullLoader))
  43. def basci_doc():
  44. from bokeh.io import show
  45. from bokeh.plotting import figure
  46. # prepare some data
  47. x = [0.1, 0.5, 1.0, 1.5, 2.0, 2.5, 3.0]
  48. y0 = [i ** 2 for i in x]
  49. y1 = [10 ** i for i in x]
  50. y2 = [10 ** (i ** 2) for i in x]
  51. # output to static HTML file
  52. # output_file("log_lines.html")
  53. # create a new plot
  54. p = figure(
  55. tools="pan,box_zoom,reset,save",
  56. y_axis_type="log", y_range=[0.001, 10 ** 11], title="log axis example",
  57. x_axis_label='sections', y_axis_label='particles'
  58. )
  59. # add some renderers
  60. p.line(x, x, legend_label="y=x")
  61. p.circle(x, x, legend_label="y=x", fill_color="white", size=8)
  62. p.line(x, y0, legend_label="y=x^2", line_width=3)
  63. p.line(x, y1, legend_label="y=10^x", line_color="red")
  64. p.circle(x, y1, legend_label="y=10^x", fill_color="red", line_color="red", size=6)
  65. p.line(x, y2, legend_label="y=10^x^2", line_color="orange", line_dash="4 4")
  66. # show the results
  67. show(p)
  68. # save(p)
  69. def datatable():
  70. from datetime import date
  71. from random import randint
  72. from bokeh.io import show
  73. from bokeh.models import ColumnDataSource, DataTable, DateFormatter, TableColumn
  74. data = dict(
  75. dates=[date(2014, 3, i + 1) for i in range(10)],
  76. downloads=[randint(0, 100) for i in range(10)],
  77. )
  78. source = ColumnDataSource(data)
  79. columns = [
  80. TableColumn(field="dates", title="Date", formatter=DateFormatter()),
  81. TableColumn(field="downloads", title="Downloads"),
  82. ]
  83. data_table = DataTable(source=source, columns=columns, width=400, height=280)
  84. show(data_table)
  85. def widgets():
  86. from bokeh.io import show
  87. from bokeh.models import Select, CheckboxButtonGroup, Button, CheckboxGroup, ColorPicker, Dropdown, \
  88. FileInput, MultiSelect, RadioButtonGroup, RadioGroup, Slider, RangeSlider, TextAreaInput, TextInput, Toggle, \
  89. Paragraph, PreText, Div
  90. put_text('Button')
  91. button = Button(label="Foo", button_type="success")
  92. show(button)
  93. put_text('CheckboxButtonGroup')
  94. checkbox_button_group = CheckboxButtonGroup(
  95. labels=["Option 1", "Option 2", "Option 3"], active=[0, 1])
  96. show(checkbox_button_group)
  97. put_text('CheckboxGroup')
  98. checkbox_group = CheckboxGroup(
  99. labels=["Option 1", "Option 2", "Option 3"], active=[0, 1])
  100. show(checkbox_group)
  101. put_text('ColorPicker')
  102. color_picker = ColorPicker(color="#ff4466", title="Choose color:", width=200)
  103. show(color_picker)
  104. put_text('Dropdown')
  105. menu = [("Item 1", "item_1"), ("Item 2", "item_2"), None, ("Item 3", "item_3")]
  106. dropdown = Dropdown(label="Dropdown button", button_type="warning", menu=menu)
  107. show(dropdown)
  108. put_text('FileInput')
  109. file_input = FileInput()
  110. show(file_input)
  111. put_text('MultiSelect')
  112. multi_select = MultiSelect(title="Option:", value=["foo", "quux"],
  113. options=[("foo", "Foo"), ("bar", "BAR"), ("baz", "bAz"), ("quux", "quux")])
  114. show(multi_select)
  115. put_text('RadioButtonGroup')
  116. radio_button_group = RadioButtonGroup(
  117. labels=["Option 1", "Option 2", "Option 3"], active=0)
  118. show(radio_button_group)
  119. put_text('RadioGroup')
  120. radio_group = RadioGroup(
  121. labels=["Option 1", "Option 2", "Option 3"], active=0)
  122. show(radio_group)
  123. put_text('Select')
  124. select = Select(title="Option:", value="foo", options=["foo", "bar", "baz", "quux"])
  125. show(select)
  126. put_text('Slider')
  127. slider = Slider(start=0, end=10, value=1, step=.1, title="Stuff")
  128. show(slider)
  129. put_text('RangeSlider')
  130. range_slider = RangeSlider(start=0, end=10, value=(1, 9), step=.1, title="Stuff")
  131. show(range_slider)
  132. put_text('TextAreaInput')
  133. text_input = TextAreaInput(value="default", rows=6, title="Label:")
  134. show(text_input)
  135. put_text('TextInput')
  136. text_input = TextInput(value="default", title="Label:")
  137. show(text_input)
  138. put_text('Toggle')
  139. toggle = Toggle(label="Foo", button_type="success")
  140. show(toggle)
  141. put_text('Div')
  142. div = Div(text="""Your <a href="https://en.wikipedia.org/wiki/HTML">HTML</a>-supported text is initialized with the <b>text</b> argument. The
  143. remaining div arguments are <b>width</b> and <b>height</b>. For this example, those values
  144. are <i>200</i> and <i>100</i> respectively.""",
  145. width=200, height=100)
  146. show(div)
  147. put_text('Paragraph')
  148. p = Paragraph(text="""Your text is initialized with the 'text' argument. The
  149. remaining Paragraph arguments are 'width' and 'height'. For this example, those values
  150. are 200 and 100 respectively.""",
  151. width=200, height=100)
  152. show(p)
  153. put_text('PreText')
  154. pre = PreText(text="""Your text is initialized with the 'text' argument.
  155. The remaining Paragraph arguments are 'width' and 'height'. For this example,
  156. those values are 500 and 100 respectively.""",
  157. width=500, height=100)
  158. show(pre)
  159. def target():
  160. from bokeh.io import output_notebook
  161. from bokeh.io import show
  162. output_notebook(verbose=False, notebook_type='pywebio')
  163. put_markdown('## Basic doc')
  164. basci_doc()
  165. put_markdown('## App')
  166. show(bkapp, notebook_url='localhost:8080')
  167. put_markdown('## App again')
  168. show(bkapp, notebook_url='localhost:8080')
  169. put_markdown('## Widgets')
  170. widgets()
  171. def test(server_proc: subprocess.Popen, browser: Chrome):
  172. time.sleep(3)
  173. percySnapshot(browser=browser, name='bokeh')
  174. def start_test_server():
  175. pywebio.enable_debug()
  176. start_server(target, port=8080, debug=True, auto_open_webbrowser=False)
  177. if __name__ == '__main__':
  178. util.run_test(start_test_server, test)