Is it possible to have the backend of a website written in python such that I can call a python method and diplay its output?

187 views Asked by At

I am trying to create a website that is centered on an algorithm I developed on python. My vision is that the user would enter something and then receive the output which has been generated with python. I am also trying to call a python file because the amount of python code that I need to implement is huge.

For simplicity sake, how would I implement this python file into a website with typing spaces:

math.py

def calc(a, b):
   c = a + b
   return c

I tried to understand brython and I used their provided example, however it did not work:

index.html

<html>
  <script src="https://raw.githack.com/brython-dev/brython/master/www/src/brython.js"></script>
  <script src="https://raw.githack.com/brython-dev/brython/master/www/src/brython_stdlib.js"></script>

  <body onload="brython()">
    <input type="text" id="text" placeholder="Enter anything in mind">
    <span id="output"></span>
      <script src="C:\ExampleCode\example.py"
              type="text/python" id="script1"></script>
  </body>
</html

example.py (I also tried ".bry")

from browser import document def show_text(e): document['output'].textContent = e.target.value; document['text'].bind('input', show_text)
2

There are 2 answers

0
Michael Cherniaev On

Browser can not directly get files from computer's file system (C:\folder\file.py) You have to write python code between script tags OR You can try to write pyton text in .js file and access it like usual js script file <script src="yourpythonscript.js">

The example of usage of brython is here

0
Yuri On

Change the html to:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
  <head>
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<script src="https://cdn.jsdelivr.net/npm/brython@3/brython.min.js">
</script>
<script src="https://cdn.jsdelivr.net/npm/brython@3/brython_stdlib.js">
</script>
  </head>
<body onload="brython()">
<script type="text/python">

<<your brython code here>>

</html>

This loads brython from a cdn and run it. Then it depends if your code can run in the brython context.

Alternatives: Flask, Falcon and many other frameworks.