-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathflet_test_markdown.py
More file actions
108 lines (87 loc) · 3.26 KB
/
flet_test_markdown.py
File metadata and controls
108 lines (87 loc) · 3.26 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
import flet as ft
from flet import TextField, GridView, Page, Column, Row, ElevatedButton, Container, Markdown, Dropdown
from deep_translator import GoogleTranslator as gt
from nltk.tokenize import word_tokenize as wt
import nltk
"""download template file here"""
'https://github.com/clueple/pythonbeginner/blob/master/flet_test_markdown.py'
"""backend stuff"""
trans = gt(source='auto', target='en')
nltk.download('averaged_perception_tagger')
"""available language details"""
lang = {
'name': ['English', 'Italian', 'German', 'French', 'Japanese', 'Chinese', 'Spanish'],
"code": ['en', 'it', 'de', 'fr', 'ja', 'zh-CN', 'es'],
"color": ['red','purple', 'black', 'light blue', 'dark blue', 'green', 'brown'],
"dname": ['English','Italiano', 'Deutsch', 'Français', '日本語', '廣東話', 'Español']
}
"""dictionary to return the language code based on language selection"""
lang_code_dict = { lang['dname'][idx]: lang['code'][idx] for idx, i in enumerate(lang['dname'])}
"""element style"""
def txt_area_style() -> dict:
return {
'multiline': True,
'expand': True,
'border_color': ft.colors.TRANSPARENT,
}
def md_style() -> dict:
return {
'value': 'whatever',
'selectable': True,
}
def drop_style() -> dict:
"""This style is going to be used for language selection dropdown list"""
return {
'options': [ft.dropdown.Option(i) for idx, i in enumerate(lang['dname'])],
'label': 'Target Language',
'hint_text': 'English',
'value': 'English'
}
"""main app"""
def main(page:Page):
# pass
"""theme settings"""
page.title = f"Warren's Markdown"
page.vertical_alignment = 'center'
page.window_height = 108*6
page.window_width = 135*6
page.horizontal_alignment = "stretch"
page.vertical_alignment = "center"
page.theme_mode = 'dark'
"""events"""
def show_result(e):
"""translate the input text area ('inp_txt') and display the result in the 'result' area"""
#return the language code based on the language selection dropbox value
lang_code = lang_code_dict[lang_select.value]
#sentence list 1 - tokenize the input text, store it in a list, 's_list'
s_list = wt(inp_txt.value)
# translate each token in s_list and translate it into target language
t_list = [str(gt(source='auto', target=lang_code,).translate(i) or ' ')for i in s_list]
result.value = t_list
page.update()
"""elements"""
inp_txt = TextField(**txt_area_style(), label='Source Text', hint_text='Enter sentence here to be translated', on_change=show_result)
result = Markdown(**md_style())
# lang_select = Dropdown(options= [ft.dropdown.Option(i) for idx, i in enumerate(lang['dname'])])
lang_select = Dropdown(**drop_style(), on_change=show_result)
"""layout"""
page.add(
#item 0 - dropdown menu for language selection
lang_select,
Row(
controls=[
#item 1 - input text area
inp_txt,
#item 2 - vertical divider
ft.VerticalDivider(color=ft.colors.YELLOW),
#item 3 - result
Container(Column(controls=[result],scroll='hidden'), alignment=ft.alignment.top_left, expand=True)
],
# other row properties
expand=True,
vertical_alignment=ft.CrossAxisAlignment.START
)
)
"""run the flet app"""
if __name__ == '__main__':
ft.app(target=main)