-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.py
More file actions
175 lines (152 loc) · 5.66 KB
/
main.py
File metadata and controls
175 lines (152 loc) · 5.66 KB
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
# main.py
from fasthtml.common import *
from fasthtml.fastapp import fast_app
import uvicorn
app, rt = fast_app()
# JavaScript with a dynamic popup notification system
js_code = """
let fileHandle; // This will store the handle to the opened file
// MODIFIED FUNCTION to create and remove a popup dynamically
function showTemporaryMessage(message) {
// 1. Create a new div element for the popup
const popup = document.createElement('div');
popup.textContent = message;
// 2. Style the popup dynamically using JavaScript
Object.assign(popup.style, {
position: 'fixed',
top: '20px',
left: '50%',
transform: 'translateX(-50%)',
backgroundColor: '#4CAF50',
color: 'white',
padding: '15px',
borderRadius: '8px',
zIndex: '1000',
transition: 'opacity 0.5s ease-out',
opacity: '1'
});
// 3. Add the popup to the page's body
document.body.appendChild(popup);
// 4. Set a timer to fade out and then remove the popup
setTimeout(() => {
popup.style.opacity = '0';
// Wait for the fade-out transition to finish before removing the element
setTimeout(() => {
document.body.removeChild(popup);
}, 500); // This duration must match the CSS transition time
}, 2000); // The popup stays visible for 2 seconds
}
async function openFile() {
try {
[fileHandle] = await window.showOpenFilePicker();
if (fileHandle) {
const file = await fileHandle.getFile();
const contents = await file.text();
const textArea = document.getElementById('editor');
const saveButton = document.getElementById('save-button');
const reloadButton = document.getElementById('reload-button');
const statusDiv = document.getElementById('status');
textArea.value = contents;
textArea.disabled = false;
saveButton.disabled = false;
reloadButton.disabled = false;
statusDiv.textContent = `Editing: ${file.name}`;
}
} catch (err) {
console.error('Error opening file:', err);
if (err.name !== 'AbortError') {
alert("Could not open file. See console for details.");
}
}
}
async function saveFile() {
if (!fileHandle) {
alert('No file is open to save.');
return;
}
try {
const textArea = document.getElementById('editor');
const newContent = textArea.value;
const writable = await fileHandle.createWritable();
await writable.write(newContent);
await writable.close();
showTemporaryMessage('File saved successfully!');
} catch (err) {
console.error('Error saving file:', err);
alert('Could not save file. See console for details.');
}
}
async function reloadFile() {
if (!fileHandle) {
alert('No file is open to reload.');
return;
}
try {
const file = await fileHandle.getFile();
const contents = await file.text();
document.getElementById('editor').value = contents;
showTemporaryMessage('File content has been reloaded from disk.');
} catch (err) {
console.error('Error reloading file:', err);
alert('Could not reload file. It may have been moved or deleted.');
}
}
"""
@rt("/")
def get():
# The static notification Div has been removed from the HTML structure
main_layout = (
Title("Direct File Editor"),
H1("Open, Edit, and Sync a Local File"),
P(
"Select a text file, modify its content, and save it back or reload from disk."
),
Div(
Button("1. Open File", onclick="openFile()"),
Button(
"2. Reload from File",
id="reload-button",
onclick="reloadFile()",
disabled=True,
),
Button(
"3. Save Changes", id="save-button", onclick="saveFile()", disabled=True
),
style="display: flex; gap: 10px;",
),
Hr(),
Div(id="status", style="font-style: italic; margin-bottom: 10px;"),
Textarea(
id="editor",
rows=20,
style="width: 90%; font-family: monospace;",
placeholder="Open a file to see its content here...",
disabled=True,
),
Script(js_code),
)
compatibility_check = (
Div(
H2("Browser Not Supported"),
P(
"This feature requires a browser with the File System Access API, "
"such as Google Chrome or Microsoft Edge."
),
id="compatibility-warning",
style=(
"display: none; position: fixed; top: 0; left: 0; width: 100%; "
"height: 100%; background-color: rgba(0,0,0,0.8); color: white; "
"text-align: center; padding-top: 20%; z-index: 2000;"
),
),
Script(
"""
if (!window.showOpenFilePicker) {
document.getElementById('compatibility-warning').style.display = 'block';
}
"""
),
)
return main_layout + compatibility_check
if __name__ == "__main__":
uvicorn.run(app, host="0.0.0.0", port=8000)