0

I'm working with Python hug API would like to create a GET API for the frontend. The frontend can download a created word document file e.g. via download button. However, after going through a documentation, I still cannot figure out a way to do it.

Here is my working script so far:

import os
import hug
from docx import Document

@hug.get("/download_submission_document")
def download_submission_document():
    file_name = 'example.docx'
    document = Document()
    document.add_heading('Test header', level=2)
    document.add_paragraph('Test paragraph')
    document.save(file_name)
    # TO DO: send a created file to frontend

I'm not sure if we can send the object right away or we have to save it first somewhere before sending the the frontend. (requirements: hug, python-docx)

I'm trying to use something like

@hug.get("/download_submission_document", output=hug.output_format.file)

but not sure how to return a file.

titipata
  • 5,321
  • 3
  • 35
  • 59

1 Answers1

1

Alright, I found a solution which is easier than I thought. Just do the following:

@hug.get("/download_submission_document", output=hug.output_format.file)
def download_submission_document():
    file_name = 'example.docx'
    document = Document()
    document.add_heading('Test header', level=2)
    document.add_paragraph('Test paragraph')
    document.save(file_name)
    return file_name

Return file_name already download the docx

titipata
  • 5,321
  • 3
  • 35
  • 59