-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathgallery.py
156 lines (130 loc) · 4.74 KB
/
gallery.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
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
import dash
import dash_core_components as dcc
import dash_html_components as html
import dash_bootstrap_components as dbc
from dash.dependencies import Input, Output, State
import plotly
import plotly.graph_objs as go
import numpy as np
import json
from .db import get_db
from .feeder import create_base_figure
from .engine import scale_and_center_polygon
def init_gallery(app):
dash_app = dash.Dash(
server=app,
routes_pathname_prefix='/part-feeder/gallery/',
update_title=None,
title='Gallery',
external_stylesheets=[dbc.themes.BOOTSTRAP]
)
dash_app.layout = html.Div([
dcc.Markdown(
children='''
# Gallery
Click on any polygon to view its plan!
''',
style={'text-align': 'center'}
),
html.Div(
dbc.Row(
[
dbc.Col(html.Button('Previous Page', id='prev', n_clicks=0, style={'text-align': 'center'})),
# dbc.Col('Show per page:', align='end'),
dbc.Col([
'Show per page:',
dcc.Dropdown(
id='num',
options=[{'label': str(int(x**2)), 'value': x} for x in range(2, 5)],
value=3,
searchable=False,
clearable=False
)],
width=2,
align='start'
),
dbc.Col(html.Button('Next Page', id='next', n_clicks=0, style={'text-align': 'center'}))
]
),
style={'text-align': 'center'},
),
html.Div(
id='page-content',
children=[]
),
dcc.Store(
id='index',
data=0,
storage_type='memory'
)
])
@dash_app.callback(
Output('page-content', 'children'),
Input('index', 'data'),
State('num', 'value')
)
def generate_grid(index, grid):
db, Part = get_db()
polygons = Part.query.slice(index, min(Part.query.count(), index+grid**2)).all()
polygons = list(map(lambda p: json.loads(p.points), polygons))
ret = [dbc.Row(
dbc.Col(
f"Showing {index + 1} to {min(index + grid ** 2, Part.query.count())} of {Part.query.count()}",
style={'text-align': 'center'}
)
)]
for i in range(grid):
row = []
for j in range(grid):
idx = i*grid+j
if idx < len(polygons):
graph = create_graph(polygons[idx])
link = html.A(children=graph, href='/part-feeder/feeder/?' + dict_to_query_string(polygons[idx]))
row.append(dbc.Col(link, width=12 // grid))
else:
row.append(dbc.Col())
ret.append(dbc.Row(row, no_gutters=True))
return dbc.Container(children=ret, fluid=True)
@dash_app.callback(
Output('index', 'data'),
Output('next', 'disabled'),
Output('prev', 'disabled'),
Input('next', 'n_clicks'),
Input('prev', 'n_clicks'),
Input('num', 'value'),
State('index', 'data')
)
def change_page(_n1, _n2, grid, index):
ctx = dash.callback_context
ret_idx = index
if ctx.triggered:
button = ctx.triggered[0]['prop_id'].split('.')[0]
if button == 'next':
ret_idx += grid ** 2
elif button == 'prev':
ret_idx = max(0, ret_idx-grid ** 2)
db, Part = get_db()
next_disabled = ret_idx + grid ** 2 >= Part.query.count()
prev_disabled = ret_idx == 0
return ret_idx, next_disabled, prev_disabled
return dash_app.server
def dict_to_query_string(points):
points = {key: [f'{key}={i}' for i in points[key]] for key in points}
return '&'.join(points['x']) + '&' + '&'.join(points['y'])
def create_graph(points: dict):
x = list(map(int, points['x']))
y = list(map(int, points['y']))
points = np.hstack((np.array(x).reshape((-1, 1)), np.array(y).reshape((-1, 1))))
points = scale_and_center_polygon(points)
draw_points = np.vstack((points, points[0]))
fig = create_base_figure()
fig.add_trace(go.Scatter(
x=draw_points[:, 0],
y=draw_points[:, 1],
mode='lines',
fill='toself',
fillcolor='#2D4262'
))
fig.update_layout(xaxis_showticklabels=False, yaxis_showticklabels=False)
# fig.update_layout(xaxis_range=(-60, 60), yaxis_range=(-60, 60))
return dcc.Graph(figure=fig, config={'displayModeBar': False, 'staticPlot': True})