1

I am trying to put some python code into a javascript function so that I can call it on a button click. I was just wondering if there was a tag or some built-in way to code python inside a javascript function? I know that I may be able to do this by asking the function to open a file or something like that, but if there is a way to contain this entire solution inside one file that would be great.

This is the code I'm trying to put into the function:

user_input = input("input text here: ")
cipher_key = int(input("input cipher key here: "))
for x in user_input:
    if x == " ":
        print(x)
    else:
        ord_num = ord(x)
        alf_num = ord_num - 97 + cipher_key
        en_num = alf_num % 26 + 97
        print(chr(en_num))
Kellen
  • 23
  • 1
  • 6
  • 4
    `onclick` can only run client code, Python runs on the server. – Barmar Nov 30 '20 at 22:28
  • 1
    There is likely a python-like library for javascript. Basically ported functionality. – GetSet Nov 30 '20 at 22:30
  • This really depends on the kind of python code that you want to run. If it's just simple functions and variable manipulations, you might be able to find a python interpreter for JavaScript, but if it is complex python code, like file manipulation, pygame, etc., then you'll need a backend server (like ExpressJS with node) that when a request is made to a certain url, it executes the python code using system calls and returns any `print()`'s, but that's too complicated for what you seem to want. – Samathingamajig Nov 30 '20 at 22:33
  • You may explore one of Python's webdev frameworks(Flask or Django). For example, with AJAX and Javascript you can call a Flask route that executes a command written in pure python using Javascript on the client side . – Seyi Daniel Nov 30 '20 at 22:34
  • 1
    What are you trying to do onclick? Most likely, if it's something you can do with Python, you can do it with Javascript as well. – M-Chen-3 Nov 30 '20 at 22:37
  • @Barmar onclick can actually call a Flask endpoint using AJAX. All you need do is point AJAX URL to the flask endpoint – Seyi Daniel Nov 30 '20 at 22:38
  • 1
    @SeyiDaniel The flask endpoint runs on the server. – Barmar Nov 30 '20 at 22:41
  • @Barmar I understand perfectly, but your response makes it look like there can't be a communication route between the client and server-side – Seyi Daniel Nov 30 '20 at 22:44
  • 1
    No, I just meant you can't run Python (or PHP, or any other server-side language) directly in onclick. You can use AJAX to send a request to the server, and you can implement the server processing in any language you want. – Barmar Nov 30 '20 at 22:52
  • @M-Chen-3 I am coding a caesar cipher for a school project. I found the javascript code solution online but I didn't quite understand it so I re-engineered the solution in python. Was checking to see if there was a lazy easy solution to get my python in rather than researching the javascript. I'll update the question with the python code. – Kellen Nov 30 '20 at 22:56
  • @Kellen I would just suggest researching the Javascript. Try using a form, getting the value of inputs, then use `.charCodeAt` to get the Unicode value of the string (refer to this question: https://stackoverflow.com/q/10879244/13736952). Then you can just write the value you get to an empty HTML element. If you need any help with that, ask a specific and detailed question about the Javascript. – M-Chen-3 Nov 30 '20 at 23:24
  • 1
    @M-Chen-3 this is the javascript and it seems it does use .charCodeAt: let caesar => (str, key) { return str.toUpperCase().replace(/[A-Z]/g, c => String.fromCharCode((c.charCodeAt(0)-65 + key ) % 26 + 65)); } – Kellen Dec 01 '20 at 00:20

1 Answers1

2

It depends on your environment ; if you are writing a node js program you can do it as indicated here How to execute an external program from within Node.js? . If you’re writing a client side code (for a web browser) you can’t .

Edit

your code is relatively simple so you can convert your function into js. Assuming that you are writing a Nodejs code:

const readline = require("readline");
const rl = readline.createInterface({
    input: process.stdin,
    output: process.stdout
});

rl.question("input text here: ", function(user_input) {
    rl.question("input cipher key here: ", function(cipher_key) {
        rl.close();
        cipher_key = parseInt(cipher_key)
        for (let i = 0; i < user_input.length(); i++) {
            const x = user_input[i];
            if (x === " ")
                process.stdout.write(x)
            else {
                const ord_num = x.charCodeAt(0)
                const alf_num = ord_num - 97 + cipher_key
                const en_num = alf_num % 26 + 97
                process.stdout.write(String.fromCharCode(en_num))

            }
        }
    });
});
radtelbi
  • 635
  • 1
  • 6
  • 18