File size: 3,552 Bytes
4eaa05e
f416ea7
 
 
4eaa05e
fe36972
f416ea7
 
4eaa05e
f416ea7
 
 
 
c6d8c09
4eaa05e
f416ea7
 
 
 
 
 
 
 
 
 
 
b0ac0e1
f416ea7
 
 
 
 
 
 
b0ac0e1
f416ea7
 
e8d4747
f416ea7
b0ac0e1
f416ea7
 
 
 
b0ac0e1
 
f416ea7
 
 
 
 
 
 
 
e8d4747
96273dd
f416ea7
 
 
4eaa05e
4f0eada
 
f416ea7
 
 
 
 
 
d38d683
f416ea7
d38d683
f416ea7
d38d683
f416ea7
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
c6d8c09
 
2aab033
c6d8c09
4eaa05e
f416ea7
 
 
 
 
 
 
 
b0ac0e1
f416ea7
 
 
 
 
 
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
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
import pandas as pd

import os

import panel as pn
import hvplot as hv

pn.extension("bokeh", template="bootstrap")
import hvplot.pandas

from datetime import datetime
from datetime import date, timedelta

from DescriptionDict import DescriptionDict

# from dotenv import load_dotenv

# from datasets import load_dataset
from utils import load_hf_dataset  # ,upload_to_hf_dataset, download_from_hf_dataset

# Load environment variables from .env file
# load_dotenv()

# Get the Hugging Face API token from the environment; either set in .env file or in the environment directly in GitHub
HF_TOKEN = os.getenv("HF_TOKEN")

# Get the name of the  dataset for GuruFocus to read from
dataset_name_GuruFocus_input = os.getenv("dataset_name_GuruFocus_input")

@pn.cache()
def get_gurufocus(current_datetime):
    # Load lastest GuruFocus DataSet from HuggingFace Dataset which is always GuruFocus_merged.csv
    # daily_gurufocus_DF = load_dataset(dataset_name_GuruFocus_output , data_files="GuruFocus_merged.csv", split="train" , token=HF_TOKEN ).to_pandas()
    return load_hf_dataset(
        f"GuruFocus_merged_{current_datetime}.csv", HF_TOKEN, dataset_name_GuruFocus_input
    )

todaydate = pn.widgets.DatePicker(
    name="End Date",  # value=datetime(2000, 1, 1),
    value=date.today() - timedelta(days=1),
    description="Select a Date",
    end=date.today(),  # date(2023, 9, 1)
)

todaydate_readcols = datetime.today().strftime("%Y-%m-%d")
df = get_gurufocus(todaydate_readcols)
selectedcol = pn.widgets.Select(
    name="Select Ratio", value="PEG Ratio", options=list(df.columns)
)
selecteditem = pn.widgets.Select(
    name="Select Item", value="Sector", options=["Industry", "Sector"]
)
selectedmethod = pn.widgets.Select(
    name="Select Method", value="Mean", options=["Mean", "Min", "Max"]
)

ticker = pn.widgets.AutocompleteInput(
    name="Ticker", options=list(df.Ticker), placeholder="Write Ticker here همین جا"
)

ticker.value = "AAPL"

selectedhover = "Ticker"
hv.extension("bokeh")

def create_plot(selectedcol, selecteditem, todaydate, selectedmethod, ticker):
    df = get_gurufocus(todaydate)
    if selectedmethod == "Mean":
        group_them = df.groupby(selecteditem)[selectedcol].mean()
    if selectedmethod == "Min":
        group_them = df.groupby(selecteditem)[selectedcol].min()
    if selectedmethod == "Max":
        group_them = df.groupby(selecteditem)[selectedcol].max()
    df2 = df.merge(
        group_them,
        left_on=selecteditem,
        right_index=True,
        suffixes=("", f"_{selecteditem}_{selectedmethod}"),
    )
    df3 = df.query("Ticker == @ticker")
    return df2.hvplot.bar(
        x=selecteditem,
        y=f"{selectedcol}_{selecteditem}_{selectedmethod}",
        hover_cols=selectedhover,
        height=800,
        width=1800,
    ).opts(xrotation=90, fontsize={"xticks": 10}).opts(show_grid=True) * df3.hvplot.bar(
        x=selecteditem,
        y=f"{selectedcol}",
        hover_cols=selectedhover,
    )


def create_alert(selectedcol):
    text = f"### {selectedcol} \n {DescriptionDict[selectedcol]}"
    return pn.pane.Alert(text, alert_type="warning")

bound_plot = pn.bind(
    create_plot,
    selectedcol=selectedcol,
    selecteditem=selecteditem,
    todaydate=todaydate,
    selectedmethod=selectedmethod,
    ticker=ticker,
)

bound_alert = pn.bind(create_alert, selectedcol=selectedcol)
pn.Column(
    pn.Row(selectedcol, selecteditem, todaydate, selectedmethod, ticker),
    bound_plot,
    bound_alert,
).servable(title="Financial Sector Ratios Navigator")