File size: 4,616 Bytes
7b38a4d
f6ce511
7b38a4d
32ba5d5
 
 
1a84ae3
32ba5d5
 
98f2613
32ba5d5
 
 
 
 
 
1a84ae3
32ba5d5
 
 
 
 
63abdba
f6ce511
 
7b38a4d
 
32ba5d5
 
 
7b38a4d
 
 
 
 
 
 
 
32ba5d5
 
 
7b38a4d
 
f6ce511
7b38a4d
 
 
 
32ba5d5
7b38a4d
 
 
32ba5d5
7b38a4d
 
32ba5d5
 
 
 
f6ce511
32ba5d5
 
f6ce511
7b38a4d
32ba5d5
 
 
f6ce511
32ba5d5
 
 
 
 
 
 
 
f6ce511
32ba5d5
 
 
7b38a4d
 
f6ce511
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
32ba5d5
 
 
7b38a4d
32ba5d5
f6ce511
 
7b38a4d
f6ce511
7b38a4d
 
f6ce511
 
 
 
 
 
 
 
 
 
 
 
 
1a84ae3
f6ce511
1a84ae3
dc1c399
32ba5d5
f6ce511
32ba5d5
7b38a4d
f6ce511
 
7b38a4d
 
32ba5d5
7b38a4d
32ba5d5
 
7b38a4d
 
f6ce511
 
7b38a4d
32ba5d5
7b38a4d
32ba5d5
f6ce511
 
 
 
 
 
 
 
 
 
 
 
32ba5d5
 
 
 
7b38a4d
1a84ae3
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
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
import dash
from dash import html, dcc, Input, Output
import yfinance as yf
import os

# =========================
# PATH
# =========================
BASE_DIR = os.path.dirname(__file__)

# =========================
# INIT APP
# =========================
app = dash.Dash(
    __name__,
    use_pages=True,
    pages_folder=os.path.join(BASE_DIR, "pages"),
    suppress_callback_exceptions=True,
    external_stylesheets=[
        "https://cdnjs.cloudflare.com/ajax/libs/font-awesome/6.5.0/css/all.min.css"
    ]
)

server = app.server  # 🔥 IMPORTANT pour Hugging Face

app.title = "TradeLux - Plateforme de Trading"

# =========================
# TICKERS
# =========================
TICKERS = {
    "BTC-USD": "BTC/USD",
    "ETH-USD": "ETH/USD",
    "^IXIC": "NASDAQ",
    "AAPL": "AAPL",
    "GOOGL": "GOOGL"
}

# =========================
# FETCH DATA
# =========================
def fetch_ticker_data():
    data = []

    for symbol, label in TICKERS.items():
        try:
            stock = yf.Ticker(symbol)
            hist = stock.history(period="1d", interval="1m")

            if len(hist) >= 2:
                current = hist['Close'].iloc[-1]
                prev = hist['Close'].iloc[-2]

                change = (current - prev) / prev * 100
                change_class = "up" if change > 0 else "down"

                data.append({
                    "label": label,
                    "value": f"{current:,.2f}",
                    "change": f"{change:+.2f}%",
                    "class": change_class
                })

            else:
                data.append({
                    "label": label,
                    "value": "N/A",
                    "change": "0.00%",
                    "class": "down"
                })

        except Exception as e:
            print("Ticker error:", e)
            data.append({
                "label": label,
                "value": "ERR",
                "change": "0.00%",
                "class": "down"
            })

    return data

# =========================
# BACKGROUND SAFE (IMPORTANT)
# =========================
background = html.Div(
    [
        html.Div(className="trade-bg"),
        html.Div(className="grid-lines"),
        html.Div(
            [html.Div(className="particle") for _ in range(40)],
            style={
                "position": "fixed",
                "zIndex": -10,
                "pointerEvents": "none"
            }
        )
    ]
)

# =========================
# NAVBAR
# =========================
navbar = html.Div(
    className="navbar",
    children=[
        html.Div(
            className="navbar-left",
            children=[
                html.Img(src="/assets/logo.png", className="logo")
            ]
        ),
        html.Div(
            className="nav-links",
            children=[
                dcc.Link("Accueil", href="/", className="nav-link"),
                dcc.Link("Marchés", href="/actions", className="nav-link"),
                dcc.Link("Analyse", href="/data", className="nav-link"),
            ]
        ),
    ]
)

# =========================
# LAYOUT
# =========================
app.layout = html.Div([

    # Background FIXED (non bloquant)
    background,

    # Ticker dynamique
    html.Div(id="ticker-container"),

    # Navbar
    navbar,

    # Content
    html.Div(
        className="page-content",
        children=[
            dash.page_container
        ],
        style={"zIndex": 1}  # 🔥 FIX CLICK
    ),

    # Interval
    dcc.Interval(id="interval", interval=60000, n_intervals=0)

])

# =========================
# CALLBACK TICKER (FIX STRUCTURE)
# =========================
@app.callback(
    Output("ticker-container", "children"),
    Input("interval", "n_intervals")
)
def update_ticker(n):

    data = fetch_ticker_data()

    items = [
        html.Div(className="ticker-item", children=[
            html.Span(d["label"]),
            html.Span(d["value"], className="ticker-value"),
            html.Span(d["change"], className=f"ticker-change {d['class']}")
        ])
        for d in data
    ]

    return html.Div(
        className="ticker-wrap",
        children=[
            html.Div(
                className="ticker-inner",
                children=[
                    html.Div(className="ticker-set", children=items),
                    html.Div(className="ticker-set", children=items)  # duplication scroll
                ]
            )
        ]
    )

# =========================
# RUN
# =========================
if __name__ == "__main__":
    app.run(host="0.0.0.0", port=7860, debug=True)