Download text returned by a different function within server side

I wrote a bit to run 3 different anonimisation functions on one piece of text.
I want the user to download only the version that is best suited to his/her needs.
How I can capture the output of the function that produces it?

At the moment it downloads each of the instances.

I am new to Shiny for Python and your help would be very much appreciated.

This is the code I wrote:

import pandas as pd
from shiny import ui, render, reactive, App, Inputs, Outputs, Session,ui
from shiny.types import FileInfo
import shinyswatch
import io
import asyncio
import os
import shiny.experimental as x
from datetime import datetime
import matplotlib.pyplot as plt
from func import anonymize_text_spacy, anonymize_text_nltk, anonymize_text_llama ## anon functions called from a different file.##
from docx import Document
## Preparation


app_ui = ui.page_fluid(
    ui.layout_sidebar(
        ui.panel_sidebar(
            ui.input_selectize("models", "Select Anonimisation model", choices= ["Spacy","NLTK","AI"]),
            ui.input_text("txt", "Insert your text"),
            ui.p(ui.input_action_button("LoadP", "Click to Run Anonimisation", class_="btn-primary")),

        ),
        
    ),
    ui.panel_main(
        ui.row(
            ui.column(6, ui.output_text("original")),
            ui.column(6, ui.output_text("anon")),
            ),
        ui.row(
            ui.output_ui("down")

        ),
        ),

    )




def server(input: Inputs, output: Outputs, session: Session):
    download_dir = os.path.join(os.path.expanduser('~'), 'Downloads')

    @output
    @render.text
    @reactive.event(input.LoadP)
    async def original():
        return input.txt()

    @output
    @render.text
    @reactive.event(input.LoadP)
    async def anon():
        if input.models()=="Spacy":
           res = anonymize_text_spacy(input.txt())
        elif input.models()=="NLTK":
           res = anonymize_text_nltk(input.txt())
        elif input.models()=="AI":
           res = anonymize_text_llama(input.txt())
        
        document = Document()
        document.add_heading('Anonimised document')
        document.add_paragraph(res)
        document.save(os.path.join(download_dir, "Anon.docx"))

        return res

This topic was automatically closed 54 days after the last reply. New replies are no longer allowed.

If you have a query related to it or one of the replies, start a new topic and refer back with a link.