-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdash_app.py
More file actions
executable file
·227 lines (193 loc) · 8.31 KB
/
Copy pathdash_app.py
File metadata and controls
executable file
·227 lines (193 loc) · 8.31 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
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
from text_reco_flask.text_recognition import *
import flask
import dash
import dash_core_components as dcc
import dash_html_components as html
from dash.dependencies import Input, Output, State
import pandas as pd
import re
cereal_df = pd.read_csv("data/cereal.csv")
server = flask.Flask(__name__)
app = dash.Dash(__name__, server=server)
app.config.requests_pathname_prefix = ''
app.config.suppress_callback_exceptions = True
url_bar_and_content_div = html.Div([
dcc.Location(id='url', refresh=False),
html.Div(id='page-content')
])
layout_index = html.Div([
dcc.Link( 'Cereal Calorie Compare', href='/page-1', className='button'),
dcc.Link( 'Nutrition Facts', href='/page-2', className='button'),
])
layout_page_1 = html.Div( children=[
html.Div( children=[ html.H1( children='Cereal Calorie Compare'),
dcc.Link( children='Nutrition Facts',
href='/page-2',
className='button')],className="heading"),
dcc.Dropdown(
id='input',
options=[{'label': i, 'value': i} for i in cereal_df.name],
multi=True,
placeholder='Enter a cereal name',
value=['All-Bran','Cheerios']),
html.Div( id='output-graph'),
#dcc.Link( 'HOME',
#href='/',
#className='button home'),
],className="main-content")
words = []
layout_page_2 = html.Div( children=[
html.Div( children=[ html.H1( children='Nutrition Facts'),
dcc.Link( children= 'Calorie Compare',
href='/page-1',
className='button')],className="heading"),
dcc.Upload(
id='upload-image',
children= html.Div(['Drag and Drop or ',
html.A( 'Select Files')
]),
className = "upload_area",
# Allow multiple files to be uploaded
multiple=True
),
html.Div( id='output-image-upload',
className='preview_image'),
html.Div( id='output-graph2'),
html.Br(),
#dcc.Link( 'HOME',
#href='/',
#className='button home'),
],className="main-content")
def serve_layout():
if flask.has_request_context():
return url_bar_and_content_div
return html.Div([
url_bar_and_content_div,
layout_index,
layout_page_1,
layout_page_2,
])
app.layout = serve_layout
# Index callbacks
@app.callback(Output('page-content', 'children'),
[Input('url', 'pathname')])
def display_page(pathname):
if pathname == "/page-1":
return layout_page_1
elif pathname == "/page-2":
return layout_page_2
else:
return layout_index
# Page 1 callbacks
@app.callback(
Output(component_id='output-graph', component_property='children'),
[Input(component_id='input', component_property='value')])
def update_graph(user_input):
if not user_input:
html.H3( "Select a cereal brand.")
else:
traces = []
for i, user_input in enumerate(user_input):
df_filtered = cereal_df[cereal_df['name'] == user_input]
traces.append({'x':df_filtered.name, 'y':(df_filtered.calories/df_filtered.cups), 'name': user_input, 'type':'bar'})
return dcc.Graph(
id='user_input',
figure={
'data':traces,
'layout': {
'title': 'Calories per cup',
'paper_bgcolor': 'rgb(255,255,255,0.3)',
'plot_bgcolor': 'rgba(0,0,0,0)',
'legend': dict(orientation='h'),
'bordercolor': '#333',
}
},
config={
'displayModeBar': False
}
)
# Page 2 callbacks
def parse_contents(contents, filename, date):
words = LetterFinding("text_reco_flask/frozen_east_text_detection.pb",contents[23:], 0.05)
#only consider de first word that appears.
index = 0;
max_index=0;
max_results =0;
while(index < len(words)):
cleanString = re.sub('\W+','', words[index] )
print(cleanString)
df_filtered = cereal_df.query('name.str.contains("'+cleanString+'",False)')
results = len(df_filtered)
print(results)
if(len(cleanString) > 2 and results > max_results):
max_results = results
max_index = index
index+=1
print(words[max_index])
print(max_results)
return html.Div([
html.H5(filename),
# HTML images accept base64 encoded strings in the same format
# that is supplied by the upload
html.Div([
html.Div( children=[
dcc.Input( id='input_2',
value=words[max_index],
type='text',
style={'display': 'none'})
]),
html.Div([ html.Img( src=contents,
id='uploaded_image',
style= {'width': '300px'})], className ='uploaded_image'),
html.P(words[max_index],
style={ 'whiteSpace': 'pre-wrap', 'wordBreak': 'break-all'},
className='text_out',
)
],className ='box_and_text')
],className ='uploaded_content')
@app.callback(
Output(component_id='output-graph2', component_property='children'),
[Input(component_id='input_2', component_property='value')])
def update_graph(user_input):
if(len(user_input) >0):
cleanString = re.sub('\W+','', user_input)
df_filtered = cereal_df.query('name.str.contains("'+cleanString+'",False)')
return dcc.Graph(
id='allcereals_rating',
figure={
'data': [
#{'x': df_filtered.name, 'y': df_filtered.calories/df_filtered.cups, 'type':'bar', 'name':'Calories per serving'},
{'x': df_filtered.name, 'y': df_filtered.fiber/df_filtered.cups, 'type':'bar', 'name':'Fiber per serving', },
{'x': df_filtered.name, 'y': df_filtered.sugars/df_filtered.cups, 'type':'bar', 'name':'Sugar per serving', },
{'x': df_filtered.name, 'y': df_filtered.protein/df_filtered.cups, 'type':'bar', 'name':'Protein per serving', },
{'x': df_filtered.name, 'y': df_filtered.fat/df_filtered.cups, 'type':'bar', 'name':'Fat per serving', },
{'x': df_filtered.name, 'y': df_filtered.sodium/df_filtered.cups/1000, 'type':'bar', 'name':'Sodium per serving', },
{'x': df_filtered.name, 'y': df_filtered.carbo/df_filtered.cups, 'type':'bar', 'name':'Carbo per serving', },
],
'layout': {
'title': 'Grams in a cup',
'paper_bgcolor': 'rgb(255,255,255,0.3)',
'plot_bgcolor': 'rgba(0,0,0,0)',
'legend': dict(orientation='h'),
'bordercolor': '#333',
}
},
config={
'displayModeBar': False
},
)
@app.callback(Output('output-image-upload', 'children'),
[Input('upload-image', 'contents')],
[State('upload-image', 'filename'),
State('upload-image', 'last_modified')])
def update_output(list_of_contents, list_of_names, list_of_dates):
if list_of_contents is not None:
children = [
parse_contents(c, n, d) for c, n, d in
zip(list_of_contents, list_of_names, list_of_dates)]
return children
# @server.route('/')
# def myDashApp():
# return app
if __name__ == '__main__':
app.run_server(debug=True)