Python Forum
pyscript index error while calling input from html form
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
pyscript index error while calling input from html form
#1
Hi,
I am trying to embed a python script into an html page via pyscript. I want to call the input from an html form. In python script, if I use
input = "word"
everything works fine, however if I use
word = Element('word').value
then it gives
Error:
IndexError: range object index out of range
Of course I used an html form as follows
	<form onsubmit="return false">
	<label>Enter the word:</label>
    <input type="text" id="word">
    </form>
Any ideas and suggestions to solve this problem would be appreciated. Thank you.
Reply
#2
Hi @pyscript_dude,

I don't use pyscript, but Brython

but for what it look like, it seem that
word = Element('word').value
return an iterable and you are out of index. Maybe you need to access it trough something like

word = Element('word')[0].value
look in the documentation of pyscript what type of object return Element()

Cheers.
[Image: NfRQr9R.jpg]
Reply
#3
You should post a a woking example of you code,as PyScript is new and not many have experience with it.
So eg is this example i make normal form with input field and button that the output what typed in the the form field.
It normal to have button that submit the form,your code dos not show if this is the case.
<html>  
<head>
  <title>HTML Forms with PyScript</title>
  <link rel="stylesheet" href="https://pyscript.net/latest/pyscript.css" />
  <script defer src="https://pyscript.net/latest/pyscript.js"></script> 
</head>

<style>
  #form {
    display: inline;
    vertical-align: top;
    text-align: center;
  }

  #form input[type="text"] {
    margin: 0;
  }

  #form input[type="text"] {
    margin: 0;
    vertical-align: top;
  }

 label{
    width: 10px;
    display: block;
    white-space: nowrap;
  }
</style>

<body>
  <form id="form" onsubmit="return false">
    <label>Enter the word:</label>
    <input type="text" id="word">
  </form>
  <input py-click="sub()" type="submit" id="btn-form" value="submit">
  <p>Output:</p>
  <p id='output'></p>

  <py-script>
    def sub():
        result = Element('output')
        result.write(Element('word').value)
  </py-script>
</body>
</html>
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Index error help MRsquared 1 776 May-15-2023, 03:28 PM
Last Post: buran
  Embedding python script into html via pyscript pyscript_dude 7 1,572 Apr-16-2023, 11:17 PM
Last Post: pyscript_dude
  Tkinterweb (Browser Module) Appending/Adding Additional HTML to a HTML Table Row AaronCatolico1 0 939 Dec-25-2022, 06:28 PM
Last Post: AaronCatolico1
  Error in Using INPUT statement gunwaba 1 2,094 Jul-03-2022, 10:22 PM
Last Post: deanhystad
  I'm getting a String index out of range error debian77 7 2,361 Jun-26-2022, 09:50 AM
Last Post: deanhystad
Star I'm getting syntax error while using input function in def. yecktmpmbyrv 1 1,980 Oct-06-2021, 09:39 AM
Last Post: menator01
  Python Error List Index Out of Range abhi1vaishnav 3 2,325 Sep-03-2021, 08:40 PM
Last Post: abhi1vaishnav
  Index error - columns vs non-column Vinny 3 4,937 Aug-09-2021, 04:46 PM
Last Post: snippsat
  Error creating database with python and form? shams 3 2,387 Aug-02-2021, 02:00 PM
Last Post: deanhystad
  Error using mariadb select query with form in python? shams 2 2,024 Jul-29-2021, 12:30 PM
Last Post: shams

Forum Jump:

User Panel Messages

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