1

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)
CairoMisr
  • 81
  • 5
  • Consider using [cgi](https://docs.python.org/3/library/cgi.html). –  May 13 '21 at 15:13
  • You need some webserver backend. This could be in python, or another language you like. Your frontend could call some backend method using http, and then pass the result as json or xml so your frontend could render it. If your python returns html, it could be directly rendered – Leroy May 13 '21 at 15:13
  • 1
    download brython.js in the same folder then modify the html: – Yuri May 13 '21 at 15:19

2 Answers2

0

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

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.

Yuri
  • 511
  • 3
  • 6