<jupyter_start><jupyter_text>This notebook allows for live plotting of data from the PiDAQ  Import Required Modules<jupyter_code>import time
import pydaqhat as py
from bokeh.io import push_notebook, show, output_notebook
from bokeh.plotting import figure
from bokeh.layouts import gridplot
import time
import ipywidgets as widgets
import os

output_notebook()<jupyter_output><empty_output><jupyter_text>PlotThis cell initialises a blank plot for live plotting.<jupyter_code>from bokeh.events import ButtonClick
from bokeh.models import Button

button = Button()

def callback(event):
    print('Python:Click')

button.on_event(ButtonClick, callback)

show(button)
from IPython.display import display
button = widgets.Button(description="Stop Plotting")
output = widgets.Output()

live_file = "/tmp/bokeh_plot.pid"

def on_button_clicked(b):
    with output:
        if os.path.isfile(live_file):
            os.remove(live_file)
        print("Plotting stopped.")

button.on_click(on_button_clicked)
button.get_state()
button.on_click?
p = figure(title="Live Plot", 
           height=300, 
           width=600, 
           background_fill_color='#efefef'
          )
r = p.line(range(500), range(500), color="#8888cc", line_width=1.5, alpha=0.8)

display(button, output)
show(p, notebook_handle=True)
n = 0
while n<10:
    print(n)
    n += 1
    time.sleep(1)<jupyter_output><empty_output><jupyter_text>Start live plot This cell reads data live from the PiDAQ and plots it to the graph above. Click the stop button in Jupyter to stop plotting. If you want plotting to resume, run the initialisation cell above before this one.<jupyter_code>def plot_continous(hat):
    print("Plotting")
    with open(live_file, 'w') as fp:
        pass
    
    sample_count = 0
    x = 0
    while os.path.isfile(live_file):
        x += 1
        new = hat.a_in_scan_read(-1,0).data[-500:]
        
        r.data_source.data = {'x': range(0, len(new)), 'y': new}
        
        sample_count += len(new)

        push_notebook()
        print(x)
        time.sleep(1)
        
    hat.a_in_scan_stop()
    hat.a_in_scan_cleanup()
    print("Scan has stopped")    
    
hat = py.continous_scan_start(
    channels = [0], # Channels to use
    iepe_enable = False, # IEPE enable/disable
    sensitivity = 1000, # Sensitivity in mV/unit
    sample_rate = 20480, # Number of samples per second
    buffer_size = 1000000 # Number of samples to keep in buffer before overwriting  
)

plot_continous(hat)<jupyter_output><empty_output><jupyter_text>Stop continous scanRun this once finished with this notebook to clean up the PiDAQs internal buffer.<jupyter_code>hat.a_in_scan_stop()
hat.a_in_scan_cleanup()
        
print("Scan has stopped")<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.09655172413793103, 'fraction_numerical': 0.018874773139745917, 'mean_word_length': 3.7111111111111112, 'pattern_counts': {'":': 0, '<': 18, '<?xml version=': 0, '>': 17, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '4', 'dir': 'jupyter-structured-clean-dedup', 'id': '595211', 'license': 'MIT', 'n_tokens_mistral': 956, 'n_tokens_neox': 903, 'n_words': 240, 'path': 'Notebooks/Bokeh Live Plot.ipynb', 'repo_name': 'uwasystemhealth/PyDAQHAT', 'size': '2755'}