[Python] How can I reuse a function in NiceGUI slots without client-side code duplication?

Discussão em 'Python' iniciado por Stack, Fevereiro 24, 2025.

  1. Stack

    Stack Membro Participativo

    NiceGUI documentation on conditional formatting in Table suggests the following:

    table.add_slot('body-cell-age', '''
    <q-td key="age" :props="props">
    <q-badge :color="props.value < 21 ? 'red' : 'green'">
    {{ props.value }}
    </q-badge>
    </q-td>
    ''')


    This has a very simple logic, namely props.value < 21 ? 'red' : 'green'

    I do want to put way more advanced formatting logic here, and reuse it across the entire app - in many slots of many different components. I do not want to just inline the same code snippet for each slot as this will result in a lot of extra code duplication on the client side. Instead, I want to define a function once, and call it from each slot.

    How can I do it?

    I tried attaching a function to window by adding <script> with ui.add_head_html but it was not recognized in my slot.

    I tried using custom VUE component for my slot definition but then it doesn't appear to populate props.

    Continue reading...

Compartilhe esta Página