Skip to content

Commit

Permalink
fix PDF export for static notebooks (#289)
Browse files Browse the repository at this point in the history
  • Loading branch information
pplonski committed May 23, 2023
1 parent 732a257 commit c36fef4
Show file tree
Hide file tree
Showing 5 changed files with 16 additions and 13 deletions.
5 changes: 3 additions & 2 deletions frontend/src/components/StatusBar.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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";
Expand Down Expand Up @@ -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) {
Expand Down Expand Up @@ -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();
Expand Down
13 changes: 7 additions & 6 deletions frontend/src/slices/tasksSlice.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<AnyAction>) => {

const sessionId = getSessionId();
Expand Down Expand Up @@ -134,7 +134,7 @@ export const scrapeSlidesHash = () => {


export const executeNotebook =
(notebookId: Number) =>
(notebookId: number) =>
async (dispatch: Dispatch<AnyAction>, getState: () => any) => {
const { widgets } = getState().widgets;
const sessionId = getSessionId();
Expand All @@ -159,7 +159,7 @@ export const executeNotebook =


export const clearTasks =
(notebookId: Number) =>
(notebookId: number) =>
async (dispatch: Dispatch<AnyAction>) => {
try {
const sessionId = getSessionId();
Expand All @@ -177,7 +177,7 @@ export const clearTasks =


export const exportToPDF =
(notebookId: Number, notebookPath: String) =>
(siteId: number, notebookId: number, notebookPath: string) =>
async (dispatch: Dispatch<AnyAction>) => {
try {
dispatch(setExportingToPDF(true));
Expand All @@ -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,
Expand All @@ -201,7 +202,7 @@ export const exportToPDF =
};

export const getPDF =
(jobId: String) =>
(jobId: string) =>
async (dispatch: Dispatch<AnyAction>) => {
try {
const url = `/api/v1/get_pdf/${jobId}/`;
Expand All @@ -228,7 +229,7 @@ export const getPDF =


export const fetchExecutionHistory =
(notebookId: Number, clearPreviousHistory = true) =>
(notebookId: number, clearPreviousHistory = true) =>
async (dispatch: Dispatch<AnyAction>) => {

dispatch(setHistoricTask({} as ITask));
Expand Down
1 change: 1 addition & 0 deletions mercury/apps/tasks/export_pdf.py
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down
5 changes: 4 additions & 1 deletion mercury/apps/tasks/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -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))
Expand Down
5 changes: 1 addition & 4 deletions setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down

0 comments on commit c36fef4

Please sign in to comment.