Skip to content

Commit

Permalink
sec: remove wkhtmltopdf
Browse files Browse the repository at this point in the history
  • Loading branch information
soofstad committed Dec 5, 2023
1 parent aaaf65b commit 2e696ef
Show file tree
Hide file tree
Showing 48 changed files with 94 additions and 105 deletions.
4 changes: 2 additions & 2 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,11 @@ web/node_modules
**/__pycache__/**
log.txt
.env
api/tests/products_data.py
api/src/tests/products_data.py
/api/algo_test_report.png
/api/report.html
/api/report.pdf
/api/test_report.png
/api/.coverage
/api/test_data/interpolate_input.csv
/api/src/test_data/interpolate_input.csv
.python-version
8 changes: 3 additions & 5 deletions api/Dockerfile
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
FROM python:3.10-slim

ENV PYTHONUNBUFFERED=1
ENV FLASK_APP=/app/app.py
ENV PYTHONPATH=/app
ENV FLASK_APP=/app/src/app.py
ENV PYTHONPATH=/app/src
ENV PATH="/app/.venv/bin:${PATH}"

RUN pip install --upgrade pip && \
Expand All @@ -15,10 +15,8 @@ RUN apt-get update && apt-get install -y \
texlive-latex-base \
texlive-fonts-extra \
texlive-fonts-recommended \
texlive-latex-extra \
wkhtmltopdf
texlive-latex-extra

#RUN apt-get install -y libblas-dev liblapack-dev

WORKDIR /app
RUN chown -R 1000:1000 /app
Expand Down
2 changes: 1 addition & 1 deletion api/docker-entrypoint.sh
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ if [ "$1" = 'api' ]; then
--log-file=- \
--workers 4 \
--timeout 60 \
app:app
src/app:app
fi
else
exec "$@"
Expand Down
69 changes: 23 additions & 46 deletions api/poetry.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 0 additions & 2 deletions api/pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,8 @@ xlrd = "^2.0.1"
azure-storage-blob = "^12.19.0"
PyJWT = "^2.8.0"
cachetools = "^5.3.2"
pypandoc = "^1.12"
vcrpy = "^5.1.0"
matplotlib = "^3.8.2"
pdfkit = "^1.0.0"
gunicorn = "^21.2.0"
Flask = "^3.0.0"

Expand Down
File renamed without changes.
3 changes: 2 additions & 1 deletion api/app.py → api/src/app.py
Original file line number Diff line number Diff line change
@@ -1,13 +1,14 @@
import traceback

from flask import Flask, Response, request, send_file

from calculators.bridge import SIZE_STEPS
from config import Config
from controllers.combination import bridge_from_combination
from controllers.optimal_bridge import bridgeRequestHandler
from controllers.optimizer import optimizer_request_handler
from controllers.products import products_get
from controllers.report import create_report
from flask import Flask, Response, request, send_file
from util.authentication import authorize
from util.sync_share_point_az import sync_all

Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
from math import sqrt

from cachetools import LFUCache, cached

from classes.product import Product
from util.enums import BridgeOption

Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
import csv
from copy import copy

from calculators.bridge import SIZE_STEPS
from numpy import log

from calculators.bridge import SIZE_STEPS


def lookup_smaller(table: dict, value: float):
n = [i for i in table.keys() if i <= value]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@

import numpy as np
from cachetools import LFUCache, cached

from calculators.bridge import SIZE_STEPS, calculate_blend_cumulative
from classes.product import Product

Expand Down
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
from calculators.bridge import theoretical_bridge
from flask import Response

from calculators.bridge import theoretical_bridge


def bridgeRequestHandler(option: str, value: int):
if not value or not option:
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
from flask import Response

from calculators.bridge import theoretical_bridge
from calculators.optimizer import Optimizer
from classes.product import Product
from controllers.products import products_get
from flask import Response


def optimizer_request_handler(
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
from cachetools import TTLCache, cached
from config import Config
from flask import Response

from config import Config
from util.azure_table import get_service


Expand Down
38 changes: 20 additions & 18 deletions api/controllers/report.py → api/src/controllers/report.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import subprocess
import tempfile
from datetime import datetime

import pypandoc
from config import Config
from plots.bridge import bridge_plot
from plots.evolution import evolution_plot
from plots.products_pie import products_pie
Expand Down Expand Up @@ -91,25 +91,27 @@ def as_html(report: Report, pie_chart, bridge_graph, fitness_plot) -> str:
</body>"""


OUT_PDF = "/tmp/report.pdf" # noqa: S108


def create_report(request: dict, bridge: bool = True):
report: Report = Report().from_dict(request)
pie_chart = products_pie(report.products)
bridge_graph = bridge_plot(report.products, report.bridging_mode, report.bridging_value) if bridge else ""
fitness_plot = evolution_plot(report.curve)
html = as_html(report, pie_chart, bridge_graph, fitness_plot)
with open(f"{Config.HOME_DIR}/report.html", "w") as report_html:
report_html.write(html)
pypandoc.convert_file(
f"{Config.HOME_DIR}/report.html",
"pdf",
format="html",
outputfile=f"{Config.HOME_DIR}/report.pdf",
extra_args=[
"--metadata title='LCM Report'" "--to",
"html",
"--css",
f"{Config.HOME_DIR}/util/report.css",
"--pdf-engine-opt=--enable-local-file-access",
],
)
return f"{Config.HOME_DIR}/report.pdf"

with tempfile.NamedTemporaryFile("w") as html_file:
html_file.write(html)
try:
subprocess.run(
[f"pandoc {html_file.name} -f html -o {OUT_PDF}"],
check=True,
shell=True, # noqa: S602
capture_output=True,
)
except subprocess.CalledProcessError as ex:
print(ex.stderr.decode())
raise ex

return OUT_PDF
3 changes: 2 additions & 1 deletion api/plots/bridge.py → api/src/plots/bridge.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,11 @@
from io import BytesIO

import matplotlib.pyplot as plt
from matplotlib.ticker import ScalarFormatter

from calculators.bridge import SIZE_STEPS, calculate_blend_cumulative, theoretical_bridge
from classes.product import Product
from controllers.products import products_get
from matplotlib.ticker import ScalarFormatter


def bridge_plot(products: dict, mode, value) -> str:
Expand Down
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,11 @@

import matplotlib.pyplot as plt
import numpy as np
from matplotlib.ticker import ScalarFormatter

from calculators.bridge import SIZE_STEPS, theoretical_bridge
from calculators.optimizer import Optimizer
from config import Config
from matplotlib.ticker import ScalarFormatter
from util.enums import BridgeOption


Expand Down
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
Empty file added api/src/util/__init__.py
Empty file.
5 changes: 3 additions & 2 deletions api/util/authentication.py → api/src/util/authentication.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,11 @@
import jwt
import requests
from cachetools import TTLCache, cached
from classes.user import User
from config import Config
from flask import abort, g, request
from jwt.algorithms import RSAAlgorithm

from classes.user import User
from config import Config
from util.exceptions import AuthenticationException


Expand Down
3 changes: 2 additions & 1 deletion api/util/azure_blobs.py → api/src/util/azure_blobs.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,11 @@
from pathlib import Path

from azure.storage.blob import BlobProperties, ContainerClient
from xlrd.sheet import Sheet

from config import Config
from util.azure_table import process_meta_blob, sanitize_row_key
from util.excel import excel_raw_file_to_sheet, sheet_to_bridge_dict
from xlrd.sheet import Sheet


def get_container_client() -> ContainerClient:
Expand Down
1 change: 1 addition & 0 deletions api/util/azure_table.py → api/src/util/azure_table.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@

from azure.common import AzureConflictHttpError
from azure.cosmosdb.table.tableservice import TableService

from config import Config


Expand Down
File renamed without changes.
File renamed without changes.
File renamed without changes.
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,9 @@
import requests
from azure.common import AzureMissingResourceHttpError
from azure.cosmosdb.table.tableservice import TableBatch, TableService
from config import Config
from urllib3.exceptions import HeaderParsingError

from config import Config
from util.azure_blobs import get_metadata_blob_data, get_product_blobs_data
from util.azure_table import create_table

Expand Down
File renamed without changes.
4 changes: 2 additions & 2 deletions docker-compose.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,8 @@ services:
TABLE_KEY: ${TABLE_KEY}
ports:
- "5000:5000"
# volumes:
# - ./api/:/app
volumes:
- ./api/src:/app/src

web:
build:
Expand Down
6 changes: 4 additions & 2 deletions web/src/Components/Combinations/CombinationCard.tsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import React, { useContext, useEffect, useState } from 'react'
// @ts-ignore
import { Button, Icon, Switch, Input} from '@equinor/eds-core-react'
import { Button, Icon, Switch, Input } from '@equinor/eds-core-react'
import CombinationTable from './CombinationTable'
import styled from 'styled-components'
import { Card } from './CardContainer'
Expand Down Expand Up @@ -131,7 +131,9 @@ export const CombinationCard = ({
<Card>
<div>
<CardHeader>
<Button variant='ghost_icon' onClick={()=>setIsHeaderEditable(!isHeaderEditable)}><Icon name='edit' size={16} /></Button>
<Button variant='ghost_icon' onClick={() => setIsHeaderEditable(!isHeaderEditable)}>
<Icon name='edit' size={16} />
</Button>
<Input
id={`${combination.name}`}
value={combinationName}
Expand Down
Loading

0 comments on commit 2e696ef

Please sign in to comment.