Forums

Help with the code

I am new to Python and created an app that is not running as desired. The server and error log don't point at any issues. Can somebody help? I can show the files. Much appreciated as I learn.

What do you expect to happen and what happens instead?

Thank you for responding, nkahr! You can view it at ljmst30.pythonanywhere.com. Unfortunately, the calculations are not being performed; it simply returns an empty form. Neither the server log nor the error log indicates any issue. I'm uncertain about what else to try. I appreciate any further assistance you can provide. Warm regards, Laura

You can add some logging to the relevant parts of your code to see if the data they receive and return match what you would expect.

I think I have done that. Can you see my code? Can I share it with you?

If you've added the logging, look at what it is printing out so you can compare that with what you expect to be printed.

Nothing is printing.

Then the places that you have added logging are not being executed. Try adding more logging so you can see what code is actually being run.

I do not know where or how. This is my wsgi.py code: from flask import Flask, render_template, request from datetime import datetime, timedelta

app = Flask(__name__)

@app.route('/', methods=["GET", "POST"])
def index():
    print("Inside index function")  # Add this line to indicate that the index function is being executed

    # Add more print statements to debug the data processing logic
    print("Request method:", request.method)

    # Extract the current month number
    current_date = datetime.now()
    num3 = current_date.month

    # Define a dictionary to map month numbers to month names
    month_names = {
        1: "Enero",
        2: "Febrero",
        3: "Marzo",
        4: "Abril",
        5: "Mayo",
        6: "Junio",
        7: "Julio",
        8: "Agosto",
        9: "Septiembre",
        10: "Octubre",
        11: "Noviembre",
        12: "Diciembre"
    }

    # Calculate the next month number
    next_month_num = (num3 % 12) + 1

    # Get the name of the next month
    next_month = month_names[next_month_num]

    if request.method == "POST":
        # Retrieve form values
        num1 = request.form.get("num1")
        num2 = request.form.get("num2")
        num9 = request.form.get("num9")
        num4 = request.form.get("num4")

        print("Form values:", num1, num2, num9, num4)  # Add this line to print the form values for debugging

        # Validate and convert form values to numbers
        if num2 and num4:  # Check if num2 and num4 are not empty strings
            try:
                num2 = float(num2)
                num4 = float(num4)
                if num2 <= 0 or num4 <= 0:
                    raise ValueError
            except ValueError:
                print("Invalid input: num2 and num4 must be numbers.")
                return render_template("index.html")  # Return the template to display the form again
        else:
            print("Error: Please enter values for num2 and num4.")
            return render_template("index.html")  # Return the template to display the form again

        if num9 is not None:
            try:
                num9 = float(num9)
            except ValueError:
                print("Invalid input: num9 must be a number.")
                return render_template("index.html")  # Return the template to display the form again
        else:
            print("Error: Please enter a value for num9.")
            return render_template("index.html")  # Return the template to display the form again

        # Calculate and process data
        percent_increase = round((num4 / num2 - 1) * 100, 2)
        new_rent = round((num4 / num2) * num9)

        # Calculate time lapse
        time_lapse = None
        if num1 is not None:
            try:
                num1 = int(num1)
                if num1 <= 0 or num1 > 12:
                    raise ValueError
                if num3 >= num1:
                    time_lapse = int(num3 - num1)
                else:
                    time_lapse = int(num3 + 12 - num1)

            except ValueError:
                print("Invalid input: num1 must be an integer between 1 and 12.")
                return render_template("index.html")  # Return the template to display the form again

        print("Time lapse:", time_lapse)  # Add this line to print the time lapse for debugging

        # Get the month name
        entered_month_name = month_names.get(num1, "No month selected")

        # Return the response for successful POST processing
        return render_template("index.html",
                               percent_increase=percent_increase,
                               new_rent=new_rent,
                               next_month=next_month,
                               current_month=month_names[num3],
                               entered_month_name=entered_month_name,
                               time_lapse=time_lapse)

    else:
        # Debugging statements for GET request handling
        print("Received GET request")

    # Render the template for GET requests
    return render_template("index.html")

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

Do you see anything wrong in it or how to add (more) logging?

What is the error you see in the error.log? (Log files are linked on the Web page) To add more logging, add print calls to your code -- be default they will show in the server.log.

No error in the error log Where else should I add print calls? I already have print call for all variables. Can you see the code I entered above?

It looks like the output that you're printing is going to your server log. Look for it there.

I do not know how to fix that.

Fix the logs going to the server.log? I don't think you have to. @glenn is just suggesting that you view the output of your print statements there. They're of the web app tab along with the error.log

Yes, I did. And there is nothing there.

Your print statements will be in the server.log right at the bottom