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.


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?


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

ch1ck3n (2035)

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.


@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