<jupyter_start><jupyter_text>Temperature Logger<NAME>  Service  Add package path<jupyter_code>import sys
import os
sys.path.append(os.path.join(os.path.dirname("__file__"), ".."))<jupyter_output><empty_output><jupyter_text>Import packages<jupyter_code>import time
import datetime
from templog.temp import TempReader
import matplotlib.pyplot as plt
import matplotlib.dates as dt
import asyncio
%matplotlib widget<jupyter_output><empty_output><jupyter_text>Start the TempReader service<jupyter_code>tr = TempReader()
tr.run()<jupyter_output>TempReader is running...<jupyter_text>Arguments  Import packages<jupyter_code>import ipywidgets as widgets
from IPython.display import display
output = widgets.Output()<jupyter_output><empty_output><jupyter_text>Define callbacks for widgets<jupyter_code>class Argv:
    def __init__(self, moniter_interval, ticks_interval):
        self.monitor_interval = moniter_interval
        self.ticks_interval = ticks_interval

default_moniter_interval = 10
default_ticks_interval = 10
argv = Argv(default_moniter_interval, default_ticks_interval)

def monitor_interval_value_changed(change):
    argv.monitor_interval = change.new
        
def ticks_interval_value_changed(change):
    argv.ticks_interval = change.new
display("Monitor interval: (sec)")
monitor_interval_box = widgets.IntText(default_moniter_interval)
display(monitor_interval_box, output)
monitor_interval_box.observe(monitor_interval_value_changed, "value")

display("Ticks interval: (sec)")
ticks_interval_box = widgets.IntText(default_ticks_interval)
display(ticks_interval_box, output)
ticks_interval_box.observe(ticks_interval_value_changed, "value")<jupyter_output><empty_output><jupyter_text>Plot  Plot asynchronously<jupyter_code>async def show_plot(fig, ax):
    for _ in range(argv.monitor_interval):
        df = tr.get_df()
        ts = [datetime.datetime.fromtimestamp(t) for t in df["datetime"]]
        temps = df["temp"]
        
        ax.clear()
        ax.set_title("Temperature Logger")
        ax.set_xlabel("DateTime")
        ax.set_ylabel("Temperature (°C)")
        ax.xaxis.set_major_formatter(dt.DateFormatter('%Y%m%d %H:%M:%S'))
        ax.xaxis.set_major_locator(dt.SecondLocator(interval=argv.ticks_interval) )
        ax.tick_params(axis='x', labelrotation=10)
        ax.plot(ts, temps)
        fig.canvas.draw()
        await asyncio.sleep(1)
moniter_button = widgets.Button(description="Monitor")
display(moniter_button, output)

fig = plt.figure(figsize=(10, 8))
ax = fig.add_subplot(111)
def moniter_button_clicked(b):
    loop = asyncio.get_event_loop()
    loop.create_task(show_plot(fig, ax))
        
moniter_button.on_click(moniter_button_clicked)<jupyter_output><empty_output><jupyter_text>Press the button to stop the serviceIt is important to stop the service correctly, or it may cause some unexpected error.<jupyter_code>stop_button = widgets.Button(description="Stop service")
display(stop_button, output)

def stop_button_clicked(b):
    with output:
        tr.stop()
        
stop_button.on_click(stop_button_clicked)<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.1023571197933484, 'fraction_numerical': 0.004197610590894414, 'mean_word_length': 5.838852097130243, 'pattern_counts': {'":': 0, '<': 29, '<?xml version=': 0, '>': 29, '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': '7', 'dir': 'jupyter-structured-clean-dedup', 'id': '549600', 'license': 'MIT', 'n_tokens_mistral': 1039, 'n_tokens_neox': 1024, 'n_words': 174, 'path': 'notebook/temp_log.ipynb', 'repo_name': 'foldfelis/templog', 'size': '3102'}