0

I am trying to change a variable called "sentence_count" according to user's input in a select form:

In my template, I have the following select:

  <div class="form-group">
      <h3>Length of summary (sentences):</h3>
      <select name="length">
        <option value="one">1</option>
        <option value="two">2</option>
        <option value="three">3</option>

      </select>
    </div>

I then have a variable defined in gvar.py:

class Gvars:
    def __init__(self):
        self.sentence_count = 1 

gvars = Gvars()

And use it (cross-file) in main.py:

def sum():
    if request.method == 'POST':
        select = request.form.get('articles')
        gvars.sentence_count = request.form.get('length')
        if select == "Guardian":
            select = request.form.get('length')
            if select == "1":
                return render_template('guardian.html', sentence_count=request.form.get('length')),
            if select == "2":
                return render_template('guardian.html', sentence_count=request.form.get('length'))
            if select == "3":
                return render_template('guardian.html', sentence_count=request.form.get('length'))
            return redirect(url_for("guardian"))

The variable 'sentence_count' is then used within the code to create a summary of that given length, the overall result is then under var 'summary' that appears in the template ('summary count' does not).

My question is how to make this work and how to change the sentence count variable according to the input. As for now, regardless of what the user choses in the select form, the length will always be the hardcoded '1'. I tried sentence_count = None in gvar.py, but that threw an error.

You can view the alpha app here to get an idea of the input, it takes a while to load and as you can see, there are no options for the length.

Thanks!

Barmar
  • 741,623
  • 53
  • 500
  • 612
GOONhoon
  • 55
  • 6
  • Show how you're calling the function and using the variable. – Barmar Sep 17 '21 at 02:19
  • Note that you'll set the variable to a string `one`, `two`, or `three`, not a number. – Barmar Sep 17 '21 at 02:20
  • I am using it in Sumy/Tokenizer where the default option was hardcoded (sentence_count = x). Here is the full code for the tokenizer and API fetching: https://paste.pythondiscord.com/halaxohusu.md. – GOONhoon Sep 17 '21 at 02:21
  • I mean show the structure of the whole code, where you call `sum()` and then use `gvars.sentence_count` after that. – Barmar Sep 17 '21 at 02:22
  • `if select == "1"` can never be true. The value of `select` will be `one`, `two`, or `three`. – Barmar Sep 17 '21 at 02:23
  • Why do you keep calling `request.form.get('length")` even though you've assigned it to a variable? And why 3 `if` statements that do the same thing? `if select in ('one', 'two', 'three'):` – Barmar Sep 17 '21 at 02:26
  • https://paste.pythondiscord.com/qulabicoto.kotlin here's main.py. As for what you suggested, should I edit it to `if select == "one"` etc.? – GOONhoon Sep 17 '21 at 02:26
  • Thanks for pointing that out, should be return `render_template('guardian.html', sentence_count=select)` – GOONhoon Sep 17 '21 at 02:28
  • how is `halaxhusu.py` used by `main.py`? – Barmar Sep 17 '21 at 02:28
  • If that's a separate script, it doesn't have variables that are set in `main.py`. Every script has its own variables. – Barmar Sep 17 '21 at 02:29
  • it is a separate script that ultimately creates the variables `summary1`, `summary2` and `summary3`, and is then used when rendering the template `return render_template('guardian.html', s1=summary, s2=summary2, s3=summary3` and as a Jinja placeholder in my template: `

    {{s3}}

    `. Also for that reason I created a gvar class in gvar.py that I call cross-file anywhere: `class Gvars: def __init__(self): self.sentence_count = 1 gvars = Gvars()`
    – GOONhoon Sep 17 '21 at 02:32
  • Variables don't persist between different scripts. – Barmar Sep 17 '21 at 02:37
  • I imported them via `from gvar import gvars` – GOONhoon Sep 17 '21 at 02:40
  • That doesn't matter. Every time you start a new script, the variables all start fresh. Different scripts that import `gvars.py` start with an empty `gvars` variable. – Barmar Sep 17 '21 at 02:41
  • If you want information to be available to different scripts, you need to save it in a file or database. – Barmar Sep 17 '21 at 02:42
  • So this means that the variable in gvar.py cannot be overridden because the one in main.py does not interact with it? How would I go about doing so through saving it in a file as you suggested? – GOONhoon Sep 17 '21 at 02:47
  • There are many tutorials on reading and writing files. – Barmar Sep 17 '21 at 02:49
  • Alright, thank you. Is there no easier way to modify the variable in halaxhusu.py according to the user input? – GOONhoon Sep 17 '21 at 03:18
  • I also realised that I need to execute the code AFTER user input is received in select, as the code only executes prior to the local server even starting with gunicorn. – GOONhoon Sep 17 '21 at 03:53

0 Answers0