Spreadsheet calculator + download file with input values

I have used the Documents & spreadsheets - Spreadsheet calculator | VIKTOR Documentation tutorial to create an app from an spreadsheet and I’m able to show the results in the app.
However, I would like to also allow the users to download the updated spreadsheet with their input values. What would be the best way to do this?
Thank you

Hi Fulei,

Thanks for posting, welcome to the community!!

Great that you got started on integrating your Excel sheet in a VIKTOR application.
Just this afternoon I updated the Spreadsheet Calculation Tutorial in our documentation. This implementation also includes the functionality you speak of.

To summarize it in a single snippet:

from pathlib import Path

from viktor import ViktorController
from viktor.external.spreadsheet import SpreadsheetCalculation, SpreadsheetCalculationInput
from viktor.parametrization import ViktorParametrization, NumberField, DownloadButton
from viktor.result import DownloadResult

class Parametrization(ViktorParametrization):
    a = NumberField("Parameter A")
    b = NumberField("Parameter B")
    c = NumberField("Parameter C")
    download_button = DownloadButton("Download evaluated spreadsheet", method="download_spreadsheet")

class Controller(ViktorController):
    label = 'My Entity Type'
    parametrization = Parametrization

    def get_evaluated_spreadsheet(self, params):
        inputs = [
            SpreadsheetCalculationInput('A', params.a),
            SpreadsheetCalculationInput('B', params.b),
            SpreadsheetCalculationInput('C', params.c),
        sheet_path = Path(__file__).parent / 'my_spreadsheet.xlsx'
        sheet = SpreadsheetCalculation.from_path(sheet_path, inputs=inputs)
        result = sheet.evaluate(include_filled_file=True)

        return result

    def download_spreadsheet(self, params, **kwargs):
        result = self.get_evaluated_spreadsheet(params)
        return DownloadResult(result.file_content, 'evaluated_spreadsheet.xlsx')

Let me know if that helps!

1 Like