Forums

Tutorial for the web app function?

I'm new to python and working through a book. I've reached the stage where I need a web server that can process python. For reasons I won't go into here I don't have the ability to set up IIS or Apache locally so I tried the web app function here and quickly hit a brick wall. I don't know what the packages are and tried a basic config which let me set up static pages, but instead of running the .py file it just cats it. Is there a how to guide available that doesn't assume familiarity with django etc (trying to learn one thing at a time)? Flipping ahead in the book I don't think I'll need anything too crazy, just the ability to host html files and python scripts. Thanks

For simple stuff like that, Flask is a good option. We do have a beginner-focused tutorial on setting up Flask on PythonAnywhere -- it goes into some depth with database configuration etc., but that doesn't come in until about halfway through -- the initial stages just walk you through getting a website up and running that can call some Python code when a specific button is clicked. So if you do just those first few steps and then stop when it gets on to the database stuff, you'll probably be in a good position to adapt the code to run functions from your script instead of the "add a comment to a list" stuff that it does.

Thanks for the help. I tried setting up flask as in the guide. but I'm getting the same thing as the non flask config. If I set up a static file entry it cats the script instead of running it, and will serve out an html file. If I call the script without the the static entry it cats the script and I get file not found on the html. To follow the examples in the book I need to be able to feed the path to a web browser and have the script run. The guide seems geared towards a much more complicated project with version control and whatnot. I just need simple files served. Guess I'll skip the current project and see if later chapters can get to scripts through html.

Ugh no dice. I can set up the html file and script for the next chapter, the html is served but the script is cat instead of run.

There is simpler, more specific documentation here: http://help.pythonanywhere.com/pages/Flask/

That actually looks worse than what I've got. Is there no way to just run simple scripts without setting up virtual servers and re coding everything? I'm trying to follow the examples the book gives me not reinvent the wheel. I'm way too new to this material to reverse engineer the example scripts.

I'm afraid that Flask is as simple as it gets on PythonAnywhere.

Doh. Looks like I'm finished with my book a little earlier than anticipated (the chapter after the web stuff deals with tkinter which is localhost or bust). I'll read through the examples and hope I've got the gist. Growing a skill set while broke is a steep hill to climb. Thanks for the help.

I am new to PythonAnywhere, i read the flask examples and my spp fits to the third example. 1) Import CSV file 2) do preprocess in the file 3)output the result CSV file.

In my script python file where I do the preprocess i need numpy and pandas packages which are already installed in Python Anywhere. The problem is that I don't know how to import these packages in order to run my script. I must write IMPORTz NUMPY in Flask file or in my app python file?

You need to import numpy in the file where you want to use numpy.

thanks for your response, i use Numpy and Pandas inside my script file, but if I write IMPORT NUMPY or IMPORT PANDAS inside my script file then an error appears. In FLASK file as I see it calls my script function.

So I can not understand how to adapt the code in order to run my script using flask. Without flask my script runs correctly as a desktop app. The problem is when I m trying to use flask and make it web app in Python anywhere.

My FLASK code is: (process_data is my script function)

FLASK code

from flask import Flask, make_response, request

from processing import process_data

app = Flask(__name__)
app.config["DEBUG"] = True

@app.route("/", methods=["GET", "POST"])
def file_summer_page():
    if request.method == "POST":
        input_file = request.files["input_file"]
        input_data = input_file.stream.read().decode("utf-8")
        output_data = process_data(input_data)
        response = make_response(output_data)
        response.headers["Content-Disposition"] = "attachment; filename=result.csv"
        return response

    return '''
        <html>
            <body>
                <p> Select the file you want to Preprocess with ILIOU  Data   Reduction   Method   :</p>
                <form method="post" action="." enctype="multipart/form-data">
                    <p><input type="file" name="input_file" /></p>
                    <p><input type="submit" value="Process the file" /></p>
                </form>
            </body>
        </html>
    '''

[edited by admin: code formatting]

What error do you get? Did you check your web app error logs?