Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feature/docx things #76

Draft
wants to merge 2 commits into
base: develop
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion fabry_perot/urls.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,8 @@

urlpatterns = [
path('', views.IndexPage.as_view(), name='index'),
path('graph/', views.Graph.as_view()),
path('graph/', views.ShowGraph.as_view()),
path('graph-report/', views.GraphReport.as_view()),
path('history/', views.HistoryTable.as_view()),
path('preset/', views.PresetsTable.as_view())
]
58 changes: 37 additions & 21 deletions fabry_perot/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,12 @@
import selph_light_lib as sll
from LightPipes import Begin, Intensity
from django.template.response import TemplateResponse
from django.views import View
from django.views.generic import TemplateView
from plotly.graph_objs import Figure

from misc.mixins.graph import GraphMixin
from misc.mixins.graph_report import GraphReportMixin
from misc.mixins.history import HistoryTableMixin
from misc.mixins.presets import PresetsTableMixin
from .forms import GraphForm
Expand Down Expand Up @@ -41,20 +44,21 @@ def get(self, request, *args, **kwargs) -> TemplateResponse:
return self.render_to_response(context)


# /fabry-perot/graph
class Graph(GraphMixin):
form = GraphForm
class Graph:
form_dict = {}

@staticmethod
def get_graph(form_dict: dict) -> str:
wave_length = form_dict['wave_length'] * sll.nm
glasses_distance = form_dict['glasses_distance'] * sll.mm
focal_distance = form_dict['focal_distance'] * sll.mm
stroke_difference = form_dict['stroke_difference'] * sll.nm
reflectivity = form_dict['reflectivity']
refractive_index = form_dict['refractive_index']
picture_size = form_dict['picture_size'] * sll.mm
n = form_dict['N']
def __init__(self, form_dict):
self.form_dict = form_dict

def create_figure(self) -> Figure:
wave_length = self.form_dict['wave_length'] * sll.nm
glasses_distance = self.form_dict['glasses_distance'] * sll.mm
focal_distance = self.form_dict['focal_distance'] * sll.mm
stroke_difference = self.form_dict['stroke_difference'] * sll.nm
reflectivity = self.form_dict['reflectivity']
refractive_index = self.form_dict['refractive_index']
picture_size = self.form_dict['picture_size'] * sll.mm
n = self.form_dict['N']

f = Begin(picture_size, wave_length, n)
intensity = Intensity(f)
Expand Down Expand Up @@ -90,15 +94,27 @@ def get_graph(form_dict: dict) -> str:
fig = px.imshow(intensity, color_continuous_scale=['#000000', laser_color])
fig.update_yaxes(fixedrange=True)

config = {
'displaylogo': False,
'toImageButtonOptions': {
'height': None,
'width': None
}
}
return fig


return fig.to_html(config=config, include_plotlyjs=False, full_html=False)
# /fabry-perot/graph
class ShowGraph(GraphMixin):
form = GraphForm

@staticmethod
def get_graph(form_dict: dict) -> Figure:
new_graph = Graph(form_dict=form_dict)
return new_graph.create_figure()


# /fabry-perot/graph-report
class GraphReport(GraphReportMixin):
form = GraphForm

@staticmethod
def get_graph(form_dict: dict) -> Figure:
new_graph = Graph(form_dict=form_dict)
return new_graph.create_figure()


# /fabry-perot/history
Expand Down
2 changes: 1 addition & 1 deletion interferometers/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,4 +6,4 @@ def home_page(request) -> render:


def team_page(request) -> render:
return render(request, 'pages/team.html')
return render(request, 'pages/team.html')
17 changes: 11 additions & 6 deletions misc/mixins/graph.py
Original file line number Diff line number Diff line change
@@ -1,22 +1,27 @@
from typing import Optional

from django.http import HttpResponse
from django.views import View
from plotly.graph_objs import Figure


class GraphMixin(View):
form = None

def post(self, request, *args, **kwargs) -> HttpResponse:
graph = None
def post(self, request, *args, **kwargs) -> Optional[HttpResponse]:
form = self.form(request.POST)

if form.is_valid():
form_dict = dict(form.cleaned_data)
graph = self.get_graph(form_dict)

return HttpResponse(graph)
config = {
'displaylogo': False,
'toImageButtonOptions': {
'height': None,
'width': None
}
}
return HttpResponse(graph.to_html(config=config, include_plotlyjs=False, full_html=False))

@staticmethod
def get_graph(form_dict: dict) -> Optional[str]:
def get_graph(form_dict: dict) -> Optional[Figure]:
return None
43 changes: 43 additions & 0 deletions misc/mixins/graph_report.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
import io

import docx

from typing import Optional
from django.http import HttpResponse, FileResponse
from django.views import View
from plotly.graph_objs import Figure
from docx.shared import Cm


class GraphReportMixin(View):
form = None

def post(self, request, *args, **kwargs) -> Optional[HttpResponse]:
form = self.form(request.POST)

if form.is_valid():
form_dict = dict(form.cleaned_data)
graph = self.get_graph(form_dict)

doc = docx.Document()

graph_png = io.BytesIO(graph.to_image(format='png'))
doc.add_picture(graph_png, width=Cm(15.75), height=Cm(11.25))

target_stream = io.BytesIO()
doc.save(target_stream)
target_stream.seek(0)

response = HttpResponse(
content_type='application/vnd.openxmlformats-officedocument.wordprocessingml.document')
response['Content-Disposition'] = 'attachment; filename="filename.docx"'

response.write(target_stream.read())
target_stream.close()
graph_png.close()

return response

@staticmethod
def get_graph(form_dict: dict) -> Optional[Figure]:
return None
2 changes: 2 additions & 0 deletions requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -12,3 +12,5 @@ python-ldap===3.4.3
redis==5.0.0
secret-key-generator==0.0.8
selph-light-lib==0.0.5
python-docx==1.0.1
kaleido==0.2.1
Loading
Loading