1

I have FastAPI app, with jinja2 templates. I wish to send a python function to one of my teplate, i render template this way return templates.TemplateResponse('index.html', context={"request": request, "func":func}) in index.html I'd like to use this function in javascript as onclick event, so when the user clicks the button python will do smth also with yield in order to show progress on javascript progressbar, but var func = {{ func }} doesn't work. So how can I send my python function to javascript in html (probably using jinja2 in some way)

Egor
  • 45
  • 1
  • 1
  • 5
  • You can't run python code in the browser... – Cerbrus Aug 31 '22 at 12:46
  • You can't. Python functions live in Python. Javascript functions lives in Javascript. Write Javascript that runs in your browser; or - look into something like [pyscript](https://pyscript.net/) which uses a small Python runtime inside your browser - but this then lives in your browser. In either case you write code that interacts between the browser and the server through HTTP requests. – MatsLindh Aug 31 '22 at 12:47
  • I have some long lasting python function with yield, which progress I wish to show as progressbar on html page, so how can i do it? – Egor Aug 31 '22 at 12:51
  • You will have to make your python script store the current progress somewhere on the server side, so that you can make an HTTP request from the client to read that value. – CBroe Aug 31 '22 at 12:57

0 Answers0