GuXiaopeng commited on
Commit
8cdc62c
·
verified ·
1 Parent(s): fe70762

Add SmartPaper restore bridge

Browse files
smartpaper_restore_bridge/__init__.py CHANGED
@@ -1 +1,50 @@
1
- 1
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ # -*- coding: utf-8 -*-
2
+ """Invisible browser storage bridge used by SmartPaper authentication.
3
+
4
+ The component is intentionally dependency-free. Its frontend is served by
5
+ Streamlit itself and returns only JSON-serializable status data to Python.
6
+ """
7
+
8
+ from __future__ import annotations
9
+
10
+ from pathlib import Path
11
+ from typing import Any
12
+
13
+ import streamlit.components.v1 as components
14
+
15
+ _FRONTEND_DIR = Path(__file__).resolve().parent / "frontend"
16
+ _FRONTEND_ENTRY = _FRONTEND_DIR / "index.html"
17
+ if not _FRONTEND_ENTRY.is_file():
18
+ raise RuntimeError("SmartPaper restore bridge frontend is missing.")
19
+
20
+ _COMPONENT = components.declare_component(
21
+ "smartpaper_restore_bridge",
22
+ path=str(_FRONTEND_DIR),
23
+ )
24
+
25
+
26
+ def restore_bridge(
27
+ *,
28
+ action: str,
29
+ command_id: str,
30
+ token: str = "",
31
+ expires_at: str = "",
32
+ key: str = "sp_restore_bridge",
33
+ ) -> dict[str, Any] | None:
34
+ """Read, write, or delete SmartPaper's browser recovery token.
35
+
36
+ The return value is asynchronous: the first call normally returns ``None``;
37
+ after the frontend completes the requested storage operation, Streamlit
38
+ reruns the script and this function returns a result dictionary.
39
+ """
40
+
41
+ value = _COMPONENT(
42
+ action=str(action or "read"),
43
+ command_id=str(command_id or ""),
44
+ token=str(token or ""),
45
+ expires_at=str(expires_at or ""),
46
+ height=0,
47
+ default=None,
48
+ key=key,
49
+ )
50
+ return value if isinstance(value, dict) else None