Home > Back-end >  Can i call python function within a td tag in HTML?
Can i call python function within a td tag in HTML?

Time:08-27

Can someone please help here? I would really appreciate that? I am trying to call this function so that i can get the values from the dictionary based on the function and dynamically create my table ...

t_b = {'b1': {1: ['g4', 15], 2: ['g3', 1], 3: ['g4', 1], 4: ['g4', 16]}}  
def some_ftn(t_b,s_id,g_id,b_id):
    s1g1 = t_b[b_id][s_id]
    if s1g1[0]== g_id:
        print(s1g1[1])
    else:
        print('-')
t_b = {'b1': {1: ['group4', 15], 2: ['group3', 1], 3: ['group4', 1], 4: ['group4', 16]}}
some_ftn(t_b,s_id=4,g_id='group1',b_id='b1')

        table  ="""
             <tr>   
          <td width=30%>
              <table width=100%>
                  <tr valign="top">
                      <td colspan="3" style="border: 1px solid black;">                          
                      </td>
                    </tr>
                  <tr>
                      <td style="border: 1px solid black;">
                          CITY
                      </td>
                      <td style="border: 1px solid black;">
                          AIRPORT
                      </td>
                      <td style="border: 1px solid black;">
                          type
                      </td>
                    </tr>   
                <tr>
        table  ="""
             <tr>   


            <td width=35%>
              <table width=100%>
              <tr>
                      <td style="border: 1px solid black;">
                          group1
                        </td>
                        <td style="border: 1px solid black;">"
                    ***I WANT TO CALL THIS FUNCTION HERE **** HOW CAN I DO IT 
                        {{some_ftn('b1','group1',1,dict)}}

                        </td>

CodePudding user response:

It should be like the following snippet below. Just use a formatted string literal (f-strings for short):

To use formatted string literals, begin a string with f or F before the opening quotation mark or triple quotation mark. Inside this string, you can write a Python expression between { and } characters that can refer to variables or literal values.

table  = f"""
             <tr>   


            <td width=35%>
              <table width=100%>
              <tr>
                      <td style="border: 1px solid black;">
                          group1
                        </td>
                        <td style="border: 1px solid black;">"

                        {some_ftn('b1','group1',1,dict)}

                        </td>
                        ...

CodePudding user response:

I haven't used this yet. But please check it if you specifically need to use python - PyScript

With Javascript you can do natively

  • Related