pythonhtmlpython-3.xtimeit

the result of the calculated time in python as a variable in html


I have a program which always generate different outputs. I want to add all results as a summary in my html raport and generate always new one. I mean:

from time import time
def f1():
    for n in range(100):
        pass

def f2():
    n=0
    while n<100:
        n+=1

if __name__ == "__main__":
    import timeit
    
    for i in range(5):
        a= timeit.timeit(f1, number=1000000)
        print(f'Time a {i+1}: {a: .3f} second(s)')
    
    for i in range(5):
        b= timeit.timeit(f2, number=1000000)
        print(f'Time b {i+1}: {b: .3f} second(s)')

outputs:

Time a 1:  0.612 second(s)
Time a 2:  0.618 second(s)
Time a 3:  0.615 second(s)
Time a 4:  0.615 second(s)
Time a 5:  0.611 second(s)
Time b 1:  2.189 second(s)
Time b 2:  2.206 second(s)
Time b 3:  2.374 second(s)
Time b 4:  2.517 second(s)
Time b 5:  2.494 second(s)

What is the best way to put my changeable results into a html format and generate a document?


Solution

  • You could write it to a text file/ or an external file. And then read that file from javascript. Then use the JS to update your HTML page.

    Another option would be to make a flask/django server, and setup a mini backend, and then setup a get request that will return the data in python. This can then be fetched by the "fetch" function in javascript, once again updating your HTML page.

    Hope this helps!