Dien-Hoa commited on
Commit
029ef5b
·
1 Parent(s): dd0a059
Files changed (1) hide show
  1. main.py +39 -55
main.py CHANGED
@@ -1,4 +1,5 @@
1
  from fasthtml.common import *
 
2
  from translator import languages as VALID_LANGUAGES, process_sbv_file
3
  import tempfile
4
  from starlette.requests import Request
@@ -9,47 +10,39 @@ import mimetypes
9
  import zipfile
10
  import io
11
 
12
- app, rt = fast_app()
 
13
 
14
- # Move TEMP_FILES to a separate module for better organization
15
  TEMP_FILES = {}
16
-
17
  # Extract form generation to a separate function
18
  def generate_language_form():
19
  return Form(
20
  Input(type='file', name='sbv_file', accept='.sbv'),
21
- P('Select languages for translation'),
22
- Button('Select All', type='button', id='select-all-btn', onclick='toggleAllLanguages()',
23
- style="font-size: 12px; padding: 5px 10px; margin-bottom: 10px;"),
24
- *[Div(
25
- Input(type='checkbox', name='languages', value=lang, id=lang),
26
- Label(lang, for_=lang)
27
- ) for lang in VALID_LANGUAGES],
28
- Div(
29
- Button('Translate', style="margin-top: 20px;"),
30
- style="text-align: center;"
 
 
31
  ),
 
32
  action="/upload", method='post', enctype='multipart/form-data'
33
  )
34
 
35
  @app.get('/')
36
  def home():
37
- return Main(
38
- P("Upload an SBV File:"),
39
- generate_language_form(),
40
- Script("""
41
- function toggleAllLanguages() {
42
- const checkboxes = document.querySelectorAll('input[name="languages"]');
43
- const selectAllBtn = document.getElementById('select-all-btn');
44
- const allChecked = Array.from(checkboxes).every(cb => cb.checked);
45
-
46
- checkboxes.forEach(cb => {
47
- cb.checked = !allChecked;
48
- });
49
-
50
- selectAllBtn.textContent = allChecked ? 'Select All' : 'Deselect All';
51
- }
52
- """)
53
  )
54
 
55
  # Extract file processing logic to a separate function
@@ -150,43 +143,34 @@ async def upload_file(request: Request, sbv_file: UploadFile):
150
 
151
  result = translate_and_process_sbv(content, sbv_file.filename, languages)
152
 
153
- return Main(
154
- Style("""
155
- .download-btn:hover {
156
- background-color: #3a8bbb !important;
157
- box-shadow: 0 4px 8px rgba(0,0,0,0.3) !important;
158
- }
159
- """),
160
- H1('Translations'),
161
- Div(
162
  A("Download All Translations",
163
  href=f"/download/{result['zip_filename']}",
164
  download=True,
165
- class_="download-btn",
166
- style="display: inline-block; padding: 12px 24px; background-color: #4299e1; color: white; text-decoration: none; border-radius: 5px; font-weight: bold; font-size: 16px; transition: all 0.3s; box-shadow: 0 2px 4px rgba(0,0,0,0.2);"
167
  ),
168
- style="margin-bottom: 30px; text-align: center;"
169
- ),
170
- Div(
171
- *[Div(
172
- H2(f'{lang.capitalize()} Translation'),
173
  Pre(
174
  '\n'.join(result['translations'][lang].split('\n')[:9]) + '\n...',
175
- style="background-color: #f5f5f5; padding: 10px; border-radius: 5px; max-height: 200px; overflow-y: auto;"
176
- ),
177
- A(f"Download {lang.capitalize()} Translation",
178
- href=f"/download/{os.path.basename(result['file_paths'][lang])}",
179
- download=True,
180
- class_="download-btn",
181
- style="display: inline-block; margin-top: 10px; padding: 8px 16px; background-color: #4299e1; color: white; text-decoration: none; border-radius: 3px; transition: all 0.3s;"
182
  ),
183
- style="margin-bottom: 20px; border: 1px solid #ddd; padding: 20px; border-radius: 5px;"
 
 
 
 
 
 
 
184
  ) for lang in result['translations']],
185
- style="max-height: 80vh; overflow-y: auto; display: flex; flex-direction: column; gap: 20px;"
186
  )
187
  )
188
 
189
- @rt("/download/{filename}")
190
  def get(filename: str):
191
  file_path = TEMP_FILES.get(filename)
192
  if file_path and os.path.exists(file_path):
 
1
  from fasthtml.common import *
2
+ from fasthtml.xtend import CheckboxX
3
  from translator import languages as VALID_LANGUAGES, process_sbv_file
4
  import tempfile
5
  from starlette.requests import Request
 
10
  import zipfile
11
  import io
12
 
13
+ css = Style(':root {--pico-font-size:90%,--pico-font-family: Pacifico, cursive;}')
14
+ app = FastHTML(hdrs=(picolink, css))
15
 
 
16
  TEMP_FILES = {}
 
17
  # Extract form generation to a separate function
18
  def generate_language_form():
19
  return Form(
20
  Input(type='file', name='sbv_file', accept='.sbv'),
21
+ H3('Select languages for translation'),
22
+ Fieldset(
23
+ Legend("Languages"),
24
+ Div(
25
+ *[CheckboxX(
26
+ label=lang,
27
+ value=lang,
28
+ id=f'lang_{lang}',
29
+ name='languages'
30
+ ) for lang in VALID_LANGUAGES],
31
+ class_='grid'
32
+ )
33
  ),
34
+ Button('Translate', class_="primary"),
35
  action="/upload", method='post', enctype='multipart/form-data'
36
  )
37
 
38
  @app.get('/')
39
  def home():
40
+ return Container(
41
+ Article(
42
+ H1("SBV File Translator"),
43
+ P("Upload an SBV File and select languages for translation:"),
44
+ generate_language_form(),
45
+ )
 
 
 
 
 
 
 
 
 
 
46
  )
47
 
48
  # Extract file processing logic to a separate function
 
143
 
144
  result = translate_and_process_sbv(content, sbv_file.filename, languages)
145
 
146
+ return Container(
147
+ Article(
148
+ H1('Translations'),
 
 
 
 
 
 
149
  A("Download All Translations",
150
  href=f"/download/{result['zip_filename']}",
151
  download=True,
152
+ class_="primary",
153
+ role="button"
154
  ),
155
+ *[Card(
156
+ H3(f'{lang.capitalize()} Translation'),
 
 
 
157
  Pre(
158
  '\n'.join(result['translations'][lang].split('\n')[:9]) + '\n...',
 
 
 
 
 
 
 
159
  ),
160
+ Footer(
161
+ A(f"Download {lang.capitalize()} Translation",
162
+ href=f"/download/{os.path.basename(result['file_paths'][lang])}",
163
+ download=True,
164
+ class_="secondary outline",
165
+ role="button"
166
+ )
167
+ )
168
  ) for lang in result['translations']],
169
+ class_="container-fluid" # Add this class for full-width content
170
  )
171
  )
172
 
173
+ @app.get("/download/{filename}")
174
  def get(filename: str):
175
  file_path = TEMP_FILES.get(filename)
176
  if file_path and os.path.exists(file_path):