Forums

App runs fine but as implemented throws an error

Hi,

I have written a simple python/Flask app to convert numbers to roman numerals. I have tested it locally without issue and in the console as well.

I have "mysite" setup with some basic html and am running into issues.

I have written into my script to "try" my script and if an exception is raised (like someone inputs some words or letters/character) to declare "only numbers please!".

Right now, whatever number you put into the text box the response is "only numbers please!".

The site is up, Flask seems to be working and I've reviewed the html. The only thing I can think is that somehow the html is adding something to the user input so that it trips my except: clause.

Any ideas?

Checking the error log suggests: "Error running WSGI application ModuleNotFoundError: No module named 'app' "

This doesn't make sense to me as the site is up and running so the app.py is running, but something is happening to the input.

http://avigranite.pythonanywhere.com/

How does your code look like?

You want me to post the whole thing here? I've run it in the console on pythonanywhere and I'm getting the correct results.

Here's the beginning and end of my python script, as well as my flask app

def NumToRoman(x):


"""Coverts any number to roman numeral (max 3999)"""
try:
    while x < 4000:
        result = []
        result_h = []
        result_t = []
        result_o = []

        ###dividing and categorizing digits###

        parts = [d for d in str(x)]

        if len(str(x)) == 4:
            result = int(x // 1000) * 'M'
            h = parts[1]
            t = parts[2]
            o = parts[3]

        elif len(str(x)) == 3:
            h = parts[0]
            t = parts[1]
            o = parts[2]

        elif len(str(x)) == 2:
            h = None
            t = parts[0]
            o = parts[1]

        else:
            h = None
            t = None
            o = parts[0]

...

        return togo
        break
    else:
        return "That's not a number between 1 and 3999"
except:
    return "Only numbers please!"

Here's my flask_app.py

from flask import Flask, render_template, request
import backend

app = Flask(__name__)

@app.route("/", methods = ["GET", "POST"])
def home():
    if request.method == "GET":
        return render_template("index.html")
    if request.method == "POST":
        text = request.form.get('textbox')
        return render_template("index.html", 
        output = backend.NumToRoman(text),
        user_text = text)

if __name__ == "__main__":
    app.run()

Maybe I missed something but I can't understand how it's rendering correctly on the site, working correctly in the console but not online. Thanks for any help you can offer!

I got it working, sort of. I added int() around the text in "output = backend.NumToRoman(int(text))" of the flask_app.py

The problem now is that if someone enters anything other than an integer you get a server error.

I'm gong to try to handle this in the python script somehow so the proper exceptions are raised if it's not an integer.

Suggestions welcomed!

Hi, in your error log I see something like:

File "/home/avigranite/mysite/flask_app.py", line 13, in home
  output = backend.NumToRoman(int(text)),
ValueError: invalid literal for int() with base 10: 'fd'

So it seems that your app uses slightly different code that the one you pasted (it tries to convert the text to integer before it's validated inside the NymToRoman method), could you check that?

I was testing it out, if you see the post before your last comment I mention that I got it working with that but it throws a server error if you put anything in other than a number. I'm trying to fix this problem with my exception handling with my python script.

I have put the flask_app.py back to how it is shown above.

And I have added this

    try:
    x = int(x)

before the while statement you see in the original.

I think this solved the issue. Thank you!

Glad you got that working!