Ask coding questions

← Back to all posts
How can I pass a javascript variable into a python function?
ZacharyElliott2 (1)

I made a character generator for 1st ed D&D in python and have recently wanted to add it to my website. The problem is that HTML doesn't like python and I am trying to use JavaScript as a go between. I got the variables from the form I used and just need to pass them into python. If anyone could help that would be much appreciated.

Answered by TestOP (19) [earned 5 cycles]
View Answer
Comments
hotnewtop
TestOP (19)

So this is a really depressing answer but you can’t, it’s not possible unless you use web methods like sockets or requests. ### Unless your doing something with large data bases I would suggest you use Java Script and fetch api to JSON files from a data base made with http server (on your computer) and use a free ngrok plan (or localtunnel) to tunnel the server. Make the JSON files like your data base for stats and etc and use those each time someone generates a character.

ZacharyElliott2 (1)

@TestOP Could I get the variables in php then pass them to python?

TestOP (19)

@ZacharyElliott2 Yes, I believe so, click this link --> Here

ch1ck3n (1613)

well you need a python web server (flask, django) to pass in some input at a API endpoint, process that data using python, then return the value to javascript.

TestOP (19)

@ch1ck3n yeah that works but then you need to know flask or django. There is something called aiohttp that is really easy and could return the values