From c36fef4d635c6dded06e189955abddcbbb1f4c5a Mon Sep 17 00:00:00 2001 From: Piotr Date: Tue, 23 May 2023 15:27:53 +0200 Subject: [PATCH] fix PDF export for static notebooks (#289) --- frontend/src/components/StatusBar.tsx | 5 +++-- frontend/src/slices/tasksSlice.ts | 13 +++++++------ mercury/apps/tasks/export_pdf.py | 1 + mercury/apps/tasks/views.py | 5 ++++- setup.py | 5 +---- 5 files changed, 16 insertions(+), 13 deletions(-) diff --git a/frontend/src/components/StatusBar.tsx b/frontend/src/components/StatusBar.tsx index 005bf8e9..5fe1f207 100644 --- a/frontend/src/components/StatusBar.tsx +++ b/frontend/src/components/StatusBar.tsx @@ -8,7 +8,7 @@ import { getWorkerState, getTryConnectCount, } from "../slices/wsSlice"; -import { setSiteStatus, SiteStatus } from "../slices/sitesSlice"; +import { getSiteId, setSiteStatus, SiteStatus } from "../slices/sitesSlice"; import axios from "axios"; import { exportToPDF, setExportingToPDF } from "../slices/tasksSlice"; import { setShowShareDialog } from "../slices/appSlice"; @@ -40,6 +40,7 @@ export default function StatusBar({ const wsStatus = useSelector(getWebSocketState); const workerState = useSelector(getWorkerState); const tryConnectCount = useSelector(getTryConnectCount); + const siteId = useSelector(getSiteId); useEffect(() => { if (tryConnectCount >= 5) { @@ -184,7 +185,7 @@ export default function StatusBar({ className="dropdown-item" onClick={() => { if (staticNotebook) { - dispatch(exportToPDF(notebookId, notebookPath)); + dispatch(exportToPDF(siteId, notebookId, notebookPath)); } else { dispatch(setExportingToPDF(true)); runDownloadPDF(); diff --git a/frontend/src/slices/tasksSlice.ts b/frontend/src/slices/tasksSlice.ts index bc947163..77aabcd3 100644 --- a/frontend/src/slices/tasksSlice.ts +++ b/frontend/src/slices/tasksSlice.ts @@ -104,7 +104,7 @@ export const getExportToPDFCounter = (state: RootState) => state.tasks.exportToP export const getExecutionHistory = (state: RootState) => state.tasks.executionHistory; export const fetchCurrentTask = - (notebookId: Number) => + (notebookId: number) => async (dispatch: Dispatch) => { const sessionId = getSessionId(); @@ -134,7 +134,7 @@ export const scrapeSlidesHash = () => { export const executeNotebook = - (notebookId: Number) => + (notebookId: number) => async (dispatch: Dispatch, getState: () => any) => { const { widgets } = getState().widgets; const sessionId = getSessionId(); @@ -159,7 +159,7 @@ export const executeNotebook = export const clearTasks = - (notebookId: Number) => + (notebookId: number) => async (dispatch: Dispatch) => { try { const sessionId = getSessionId(); @@ -177,7 +177,7 @@ export const clearTasks = export const exportToPDF = - (notebookId: Number, notebookPath: String) => + (siteId: number, notebookId: number, notebookPath: string) => async (dispatch: Dispatch) => { try { dispatch(setExportingToPDF(true)); @@ -188,6 +188,7 @@ export const exportToPDF = const url = `/api/v1/export_pdf/`; // convert from JS camel case to Python undescore variables const params = { + site_id: siteId, session_id: sessionId, notebook_id: notebookId, notebook_path: notebookPath, @@ -201,7 +202,7 @@ export const exportToPDF = }; export const getPDF = - (jobId: String) => + (jobId: string) => async (dispatch: Dispatch) => { try { const url = `/api/v1/get_pdf/${jobId}/`; @@ -228,7 +229,7 @@ export const getPDF = export const fetchExecutionHistory = - (notebookId: Number, clearPreviousHistory = true) => + (notebookId: number, clearPreviousHistory = true) => async (dispatch: Dispatch) => { dispatch(setHistoricTask({} as ITask)); diff --git a/mercury/apps/tasks/export_pdf.py b/mercury/apps/tasks/export_pdf.py index b4cf4f58..e71157fd 100644 --- a/mercury/apps/tasks/export_pdf.py +++ b/mercury/apps/tasks/export_pdf.py @@ -124,6 +124,7 @@ def install_chromium(): def to_pdf(html_input_file, pdf_output_file): if not pdf_export_available: + print("PDF export not available") return # make sure chromium is installed # install_chromium() diff --git a/mercury/apps/tasks/views.py b/mercury/apps/tasks/views.py index b01c58b1..25f92eef 100644 --- a/mercury/apps/tasks/views.py +++ b/mercury/apps/tasks/views.py @@ -198,11 +198,14 @@ class ExportPDF(APIView): def post(self, request): try: # check if user can access the notebook - notebook = notebooks_queryset(request).get(pk=request.data["notebook_id"]) + notebook = notebooks_queryset(request, request.data.get("site_id")).get( + pk=request.data["notebook_id"] + ) except Notebook.DoesNotExist: raise Http404() try: celery_job = export_to_pdf.delay(request.data) + print(celery_job.id) return Response({"job_id": celery_job.id}, status=status.HTTP_201_CREATED) except Exception as e: raise APIException(str(e)) diff --git a/setup.py b/setup.py index a2b42485..52a18435 100644 --- a/setup.py +++ b/setup.py @@ -21,13 +21,10 @@ def list_files(directory): long_description_content_type="text/markdown", install_requires=open("mercury/requirements.txt").readlines(), url="https://github.com/mljar/mercury", - classifiers=[ - "Programming Language :: Python :: 3", - "Operating System :: OS Independent", - ], packages=find_packages(), python_requires='>=3.8', classifiers=[ + "Operating System :: OS Independent", "Programming Language :: Python", "Programming Language :: Python :: 3.8", "Programming Language :: Python :: 3.9",