from django.shortcuts import render # Create your views here. import json from django.shortcuts import render from django.views import View from django.http import HttpResponseRedirect import plots.packing_algo as poly from bokeh.embed import file_html from bokeh.models import BoxZoomTool from django.views.generic import TemplateView import matplotlib from django.http import JsonResponse from django.http import HttpResponse import pdb;pdb.set_trace import matplotlib.pyplot as plt import mpld3 import math from plots import plot_helpers from bokeh.plotting import figure, output_file, show from bokeh.resources import CDN from bokeh.models.widgets import Panel, Tabs from bokeh.layouts import layout from bokeh.models.widgets import Tabs, Panel from bokeh.io import curdoc from bokeh.plotting import figure from bokeh.layouts import column from bokeh.models import CustomJS, ColumnDataSource from bokeh.plotting import Figure from bokeh.models import ColumnDataSource, CustomJS, Slider from bokeh.embed import components from bokeh.embed import json_item from bokeh.events import ButtonClick import numpy as np from bokeh.models import PolyDrawTool, PolyEditTool from bokeh.models.callbacks import CustomJS from bokeh.embed import json_item import json def index(request): convex_polygons = poly.create_multiple_convex_polygons(10,9) cc = poly.ConvexContainer(convex_polygons) plot_steps = cc.plot_steps(render=False) plot_steps_html = file_html(plot_steps, CDN, "my plot2") return render(request, 'plots/index_new.html', context={"plot_steps":plot_steps_html}) # 'high_classes': plot_high_class_list,}) # 'hc_container': hc_container}) class PackingRectContainer(View): def get(self, request, *args, **kwargs): cc = poly.pack_polygons( PolygonEditView.polygons) plot_steps = cc.plot_steps(render=False) plot_steps_html = file_html(plot_steps, CDN, "my plot23") #PackingPolygons.context["packed_polygons"]=plot_steps_html return render(request, 'plots/index_new.html', context={"plot_steps": plot_steps_html}) class PackingRotatedRectContainer(View): def get(self, request, *args, **kwargs): cc = poly.ConvexContainer(PolygonEditView.polygons,steps=90) plot_steps = cc.plot_steps(render=False) plot_steps_html = file_html(plot_steps, CDN, "my plot23") # PackingPolygons.context["packed_polygons"]=plot_steps_html return render(request, 'plots/index_new.html', context={"plot_steps": plot_steps_html}) class PackingConvexContainer(View): # context = {} def get(self, request, *args, **kwargs): cc = poly.ConvexContainer(PolygonEditView.polygons, steps=1) plot_steps = cc.plot_steps(render=False) plot_steps_html = file_html(plot_steps, CDN, "my plot23") # PackingPolygons.context["packed_polygons"]=plot_steps_html return render(request, 'plots/index_new.html', context={"plot_steps": plot_steps_html}) def post(self, request, *args, **kwargs): angle = int(request.POST["angle"]) cc = poly.ConvexContainer(PolygonEditView.polygons, steps=angle) plot_steps = cc.plot_steps(render=False) plot_steps_html = file_html(plot_steps, CDN, "my plot23") return render(request, 'plots/index_new.html', context={"plot_steps": plot_steps_html}) class PolygonEditView(View): context={} polygons=[] plot_min_x= 0 plot_max_x= 100 plot_min_y = 0 plot_max_y= 100 colum_data_x=[] # wil be a list of lists colum_data_y=[] def get(self, request, *args, **kwargs): TOOLTIPS = [("index", "$index"), ("(x,y)", "($x{1.1}, $y{1.1})"), ] source = ColumnDataSource(data=dict(x=PolygonEditView.colum_data_x, y=PolygonEditView.colum_data_y), name='my-data-source') p = figure(x_range=(PolygonEditView.plot_min_x, PolygonEditView.plot_max_x), y_range=(PolygonEditView.plot_min_y, PolygonEditView.plot_max_y), width=1000, height=1000, title='Poly Edit Tool', tooltips=TOOLTIPS) p.aspect_ratio = 1 p1 = p.patches("x", "y", fill_alpha=0.4, source=source, fill_color="red") c1 = p.circle([], [], size=10, color='red', ) draw_tool = PolyDrawTool(renderers=[p1]) edit_tool = PolyEditTool(renderers=[p1], vertex_renderer=c1) p.add_tools(draw_tool, edit_tool) p.toolbar.active_drag = draw_tool response = file_html(p, CDN, "my plot") PolygonEditView.context["response"] = response return render(request, 'plots/test.html', PolygonEditView.context) def post(self, request, *args, **kwargs): test = request.POST["your_name"] dict_poly = json.loads(test) print(dict_poly["x"]) print(dict_poly["y"]) PolygonEditView.colum_data_x.clear() PolygonEditView.colum_data_y.clear() for poly_x in dict_poly["x"]: PolygonEditView.colum_data_x.append(poly_x) for poly_y in dict_poly["y"]: PolygonEditView.colum_data_y.append(poly_y) polygon_list=[] polygons_x_y_tuple = list(zip(dict_poly["x"], dict_poly["y"])) # polygon_x_list = [] # polygon_y_list = [] for x,y in polygons_x_y_tuple: polygon_list.append(list(zip(x,y))) # for x,y in polygons_x_y_tuple: # polygon_x_list.append(x) # polygon_y_list.append(y) print(polygon_list) real_polygons=[] polygon_max_x_list=[] polygon_min_x_list = [] polygon_min_y_list = [] polygon_max_y_list=[] for polygon in polygon_list: if len(polygon)<3: continue if polygon[0]!= polygon[-1]: polygon.append(polygon[0]) polygon2 = poly.ConvexPolygon(polygon) print("konkav",list(polygon2.shell)) #polygon_parent_class = polygon2.convex_hull #polygon2 = poly.Polygon2(list(polygon_parent_class.exterior.coords)) polygon_max_x_list.append(polygon2.max_x) polygon_min_x_list.append(polygon2.min_x) polygon_max_y_list.append(polygon2.max_y) polygon_min_y_list.append(polygon2.min_y) real_polygons.append(polygon2) # for setting the plot PolygonEditView.polygons = real_polygons PolygonEditView.plot_max_y = max(polygon_max_y_list) PolygonEditView.plot_min_y = min(polygon_min_y_list) PolygonEditView.plot_max_x = max(polygon_max_x_list) PolygonEditView.plot_min_x = min(polygon_min_x_list) polygons_single_plot = poly.plot_polygons_in_single_plot(real_polygons,plot_height=1000, plot_width=2000,render=False) plot = poly.plot_polygons(real_polygons,render=False) # print(type(test)) # p = figure() # p.multi_line("x", "y", source=source) polygons_plot_html= file_html(plot, CDN, "my plot") polygons_single_plot_html = file_html(polygons_single_plot, CDN, "single plot") #self.polygons.append(response2) PolygonEditView.context["polygons_plot"]= polygons_plot_html PolygonEditView.context["polygons_single_plot"] = polygons_single_plot_html return HttpResponseRedirect('/test/')