Python Forum
Embedding python script into html via pyscript
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Embedding python script into html via pyscript
#3
I have updated the code as follows:
<html>
  <head>
    <link rel="stylesheet" href="https://pyscript.net/latest/pyscript.css" />
    <script defer src="https://pyscript.net/latest/pyscript.js"></script>
  </head>
  <body>
 
    <input type="text" id="input_str"/>
    <input type="number" id="n"/>
    <button id="submit-button" type="button" py-click="my_function()">OK</button>
    <div id="test-output"></div>
 
    <p>Output:</p>
    <p id='output'></p>
 
    <py-config>
      packages = ["numpy"]
    </py-config>
 
    <py-script>
    import numpy as np
    from pyscript import Element
 
    def my_function():
        input_str = Element('input_str').value
        n = int(Element('n').value)
 
    input_list = input_str.split(",")
    w = []
    for num in input_list:
      w.append(int(num))
     
    rows = len(w) // n
    A = np.zeros((rows, n))
 
    for i in range(len(w)):
      if (w[i] % n == 0):
        A[w[i]//n - 1, n-1] = int(i+1)
    else:
        A[w[i]//n, w[i]%n-1] = int(i+1)
 
        result_place = Element('output')
        result_place.write(str(A))
 
    </py-script>
  </body>
</html>
However, now it gives the following error message:

Error:
NameError: name 'input_str' is not defined
Any ideas to fix it?
Reply


Messages In This Thread
RE: Embedding python script into html via pyscript - by pyscript_dude - Apr-16-2023, 04:08 PM

Possibly Related Threads…
Thread Author Replies Views Last Post
  speed up getting embedding from bert model for large set of text veda 7 2,519 May-27-2024, 08:28 AM
Last Post: Pedroski55
  Is there a *.bat DOS batch script to *.py Python Script converter? pstein 3 8,507 Jun-29-2023, 11:57 AM
Last Post: gologica
  pyscript index error while calling input from html form pyscript_dude 2 2,139 May-21-2023, 08:17 AM
Last Post: snippsat
  Tkinterweb (Browser Module) Appending/Adding Additional HTML to a HTML Table Row AaronCatolico1 0 1,998 Dec-25-2022, 06:28 PM
Last Post: AaronCatolico1
  C++ python embedding comarius 0 1,323 Aug-26-2022, 02:01 AM
Last Post: comarius
  reading html and edit chekcbox to html jacklee26 5 4,581 Jul-01-2021, 10:31 AM
Last Post: snippsat
Question Embedding a python file online Dreary35 0 2,019 Jun-10-2021, 05:05 PM
Last Post: Dreary35
  Embedding python cause crash when use boost::asio multi threading udvatt108 0 2,245 Oct-04-2020, 03:15 PM
Last Post: udvatt108
  Need help merging/embedding duckredbeard 10 5,349 Aug-13-2020, 04:48 AM
Last Post: duckredbeard
  How to kill a bash script running as root from a python script? jc_lafleur 4 8,362 Jun-26-2020, 10:50 PM
Last Post: jc_lafleur

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020