-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.py
99 lines (77 loc) · 3 KB
/
app.py
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
import base64
import os
from flask import Flask, send_from_directory
import dash
import dash_core_components as dcc
import dash_bootstrap_components as dbc
import dash_html_components as html
from dash.dependencies import Input, Output, State
from dash.exceptions import PreventUpdate
import random as rand
# Normally, Dash creates its own Flask server internally. By creating our own,
# we can create a route for downloading files directly:
server = Flask(__name__)
app = dash.Dash(server=server, external_stylesheets=[dbc.themes.BOOTSTRAP])
# dark mode = CYBORG
# normal mode = BOOTSTRAP
app.layout = html.Div(
[
html.H2("noun",id="disp-word", style={'padding':'4px'},
title="When you click the button, your word will appear here"),
html.Div([
dbc.Button("Get new word", id='get-word', n_clicks=0, color="primary",
size="lg",className="mr-1",block=True),
],style={'width': '100%', 'display': 'block','padding':'4px'}
),
html.Div([
dbc.Button("See on SignBank", id='signbank-link', n_clicks=0, color="secondary",
size="lg",className="mr-1",block=True,target="_blank",
href="https://www.auslan.org.au/dictionary/words/noun-1.html"),
],style={'width': '100%', 'display': 'block','padding':'4px'}
),
html.Div([
dcc.Dropdown( id="word-len", options=[
{'label': '4-letter words', 'value': '4'},
{'label': '5-letter words', 'value': '5'},
{'label': '6-letter words', 'value': '6'},
{'label': '7-letter words', 'value': '7'},
{'label': '8-letter words', 'value': '8'}
],
value=['4', '5'],multi=True
)
], style={'width': '100%', 'display': 'block','padding':'4px'})
],
style={"max-width": "360px","margin":"40px"},
)
# Show or hide the extra configuration panel (working)
@app.callback(
[Output("disp-word", "children"),
Output("signbank-link", "href")],
Input("get-word", "n_clicks"),
State('word-len','value'),
)
def toggle_configuration_display(n_clicks,n_letters):
filename = "data/vowel-pair.txt"
if n_clicks == 0:
raise PreventUpdate
nl = [int(n) for n in n_letters]
if len(nl) == 0:
raise PreventUpdate
nl = rand.choice(nl)
# print('Getting a '+str(nl)+"-letter word\n")
with open(filename,"rt") as file:
# assuming wordlist starts with len-4 words and we don't
# let the user ask for a longer word than what's in the list
for u in range(0,nl-3):
header = file.readline()
# print("header: " + header)
header = [int(s) for s in header.split()]
index = rand.randrange(header[1])
file.seek(header[0] + (nl+1)*index) # 2 on windows, 1 on posix
word = file.readline()
href = "https://www.auslan.org.au/dictionary/words/{}-1.html".format(word)
# print("word: " + word)
return word, href
if __name__ == "__main__":
app.run_server(debug=True, port=8888)
# app.run_server(host="0.0.0.0", port="8050", debug=True)