lilferrit commited on
Commit
61f6c42
1 Parent(s): dfa545b

implemented upload image ui

Browse files
Files changed (2) hide show
  1. eggcount/pages/home.py +116 -7
  2. eggcount/ui/ui_utils.py +18 -3
eggcount/pages/home.py CHANGED
@@ -1,22 +1,131 @@
1
- from dash import html, dcc, callback, Input, Output
 
 
 
 
 
2
 
 
 
3
  import dash
4
  import dash_bootstrap_components as dbc
 
5
 
 
6
  dash.register_page(__name__, path = "/")
7
 
8
  UPLOAD_HEIGHT = "25vh"
9
 
10
- layout = dbc.Container(
11
- children = dcc.Upload(
12
- id = 'upload-data',
13
  children = dbc.Container(
14
- "Upload Image Here",
15
- class_name = "w-100 border border-dark",
 
 
 
 
 
 
 
16
  style = {
17
  "height": UPLOAD_HEIGHT
18
  }
19
  )
20
- ),
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
21
  class_name = "text-center mt-3"
22
  )
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ from dash import html, dcc, callback, Input, Output, State
2
+ from dash.exceptions import PreventUpdate
3
+ from typing import Tuple, Any, Dict
4
+ from io import BytesIO
5
+ from PIL import Image
6
+ from pillow_heif import register_heif_opener
7
 
8
+ import plotly.express as px
9
+ import base64
10
  import dash
11
  import dash_bootstrap_components as dbc
12
+ import numpy as np
13
 
14
+ register_heif_opener()
15
  dash.register_page(__name__, path = "/")
16
 
17
  UPLOAD_HEIGHT = "25vh"
18
 
19
+ def get_initial_upload_container() -> dbc.Container:
20
+ return dcc.Upload(
21
+ id = "upload-data",
22
  children = dbc.Container(
23
+ children = [
24
+ html.Img(
25
+ src = "assets/camera.png",
26
+ alt = "camera-image",
27
+ className = "h-50"
28
+ ),
29
+ html.H2("Drag and Drop or Select Image File")
30
+ ],
31
+ class_name = "w-100 d-flex flex-column justify-content-center align-items-center",
32
  style = {
33
  "height": UPLOAD_HEIGHT
34
  }
35
  )
36
+ )
37
+
38
+ def get_new_upload_container(
39
+ image_b64: str,
40
+ file_name: str
41
+ ) -> dbc.Container:
42
+ decoded_bytes = base64.b64decode(image_b64)
43
+ image_data = BytesIO(decoded_bytes)
44
+ pil_img = Image.open(image_data)
45
+ img = np.array(pil_img)
46
+ image_fig = px.imshow(img)
47
+
48
+ return dbc.Container(
49
+ children = [
50
+ html.H3(
51
+ children = file_name,
52
+ className = "p-2 text-start",
53
+ ),
54
+ dcc.Graph(figure = image_fig),
55
+ dbc.Container(
56
+ children = dcc.Upload(
57
+ children = dbc.Button(
58
+ children = "Upload New Image",
59
+ color = "secondary"
60
+ ),
61
+ id = "upload-data"
62
+ ),
63
+ class_name = "w-100 pb-4 d-flex flex-row justify-content-center align-items-center"
64
+ )
65
+ ]
66
+ )
67
+
68
+ layout = dbc.Container(
69
+ children = [
70
+ dbc.Container(
71
+ children = get_initial_upload_container(),
72
+ id = "image-upload-container",
73
+ class_name = "m-0 p-0 border border-dark"
74
+ ),
75
+ dcc.Store(
76
+ id = "img-data-store",
77
+ storage_type = "memory"
78
+ ),
79
+ dbc.Modal(
80
+ children = [
81
+ dbc.ModalHeader(
82
+ dbc.ModalTitle("Error Processing Image File")
83
+ ),
84
+ dbc.ModalBody(
85
+ children = "",
86
+ id = "upload-modal-content"
87
+ ),
88
+ ],
89
+ is_open = False,
90
+ id = "upload-modal"
91
+ )
92
+ ],
93
  class_name = "text-center mt-3"
94
  )
95
+
96
+ @callback(
97
+ Output("image-upload-container", "children"),
98
+ Output("img-data-store", "data"),
99
+ Output("upload-modal-content", "children"),
100
+ Output("upload-modal", "is_open"),
101
+ Input("upload-data", "contents"),
102
+ State("upload-data", "filename"),
103
+ State("image-upload-container", "children"),
104
+ State("img-data-store", "data")
105
+ )
106
+ def on_image_upload(
107
+ upload_image_data: str,
108
+ upload_image_name: str,
109
+ curr_upload_chidren: Any,
110
+ curr_img_store_data: Dict,
111
+ ) -> Tuple[dbc.Container, Dict, str, bool]:
112
+ if not upload_image_data:
113
+ raise PreventUpdate
114
+
115
+ try:
116
+ content_type, content_string = upload_image_data.split(',')
117
+ next_children = get_new_upload_container(content_string, upload_image_name)
118
+
119
+ return (
120
+ next_children,
121
+ {"img": content_string},
122
+ "",
123
+ False
124
+ )
125
+ except Exception as e:
126
+ return (
127
+ curr_upload_chidren,
128
+ curr_img_store_data,
129
+ str(e),
130
+ True
131
+ )
eggcount/ui/ui_utils.py CHANGED
@@ -6,13 +6,28 @@ def get_navbar() -> dbc.Nav:
6
  return dbc.Nav(
7
  children = [
8
  dbc.NavItem(
9
- dbc.NavLink("Home")
 
 
 
 
10
  ),
11
  dbc.NavItem(
12
- dbc.NavLink("About")
 
 
 
 
 
 
 
 
 
 
 
13
  )
14
  ],
15
- class_name = "bg-dark",
16
  style = {
17
  "min-height": NAVBAR_MIN_HEIGHT
18
  }
 
6
  return dbc.Nav(
7
  children = [
8
  dbc.NavItem(
9
+ children = dbc.NavLink(
10
+ children = "Home",
11
+ href = "/",
12
+ class_name = "text-light"
13
+ )
14
  ),
15
  dbc.NavItem(
16
+ children = dbc.NavLink(
17
+ children = "Usage Guide",
18
+ href = "/guide",
19
+ class_name = "text-light"
20
+ )
21
+ ),
22
+ dbc.NavItem(
23
+ children = dbc.NavLink(
24
+ children = "About",
25
+ href = "/about",
26
+ class_name = "text-light"
27
+ )
28
  )
29
  ],
30
+ class_name = "bg-dark d-flex flex-row justify-content-start align-items-center",
31
  style = {
32
  "min-height": NAVBAR_MIN_HEIGHT
33
  }