File size: 1,637 Bytes
8f44d6d
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
import pandas as pd

import logging
from cachetools import TTLCache, cached

from shiny import Inputs, Outputs, Session
from shiny.express import module, render, ui

from .config import HISTORY_FILE, ONE_MINUTE_SEC


# Log
logging.basicConfig(level=logging.ERROR,
                    format='%(asctime)s - %(levelname)s - %(message)s')


@cached(cache=TTLCache(maxsize=3000, ttl=ONE_MINUTE_SEC*5))  # Memory
def get_history_data():
    try:
        parse_dates = ['time_of_prediction']
        df_history = pd.read_csv(HISTORY_FILE, parse_dates=parse_dates)
    except Exception as e:
        df_history = None
        logging.error(f"Oops, an error occured in the history page: {e}")

    finally:
        return df_history


# Display History Page
@module
def history_page(input: Inputs, output: Outputs, session: Session):
    # Disable loading spinners, use elegant pulse
    ui.busy_indicators.use(spinners=False)

    ui.panel_title(title=ui.h1(ui.strong("Prediction History 🕰️")),
                   window_title="History page")

    df_history = get_history_data()

    if df_history is not None:
        with ui.card():
            with ui.card_header():
                ui.h2("Explore all past predictions")

            @render.data_frame
            def _():
                return render.DataGrid(
                    df_history,
                    selection_mode="rows",
                    filters=True,
                )

    else:
        ui.notification_show(
            "🚨 There is no history file yet. Make a prediction.", duration=6, type="warning")