Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Flask go to new page on button click

I have a small flask app with an index page where the user selects a directory from a drop down menu. Once the user makes a selection and clicks "submit" on the webpage I would like to send them to a new page with info specific to that directory.

I’m trying to load the new page by simply calling the function after getting the input from the index page. My issue is that clicking submit on the webpage reloads the same index page.

Flask Code:

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

app = Flask(__name__)
dirname = os.path.dirname(sys.argv[0])

run_path = ""

@app.route("/", methods = ["GET", "POST"])
def index():
    dir_loc = "/Users/kregan1/Documents"
    dir_data = list(os.listdir(dir_loc))

    if request.method == "POST":
        run_dir = request.form.get("run_list")
        run_path = dir_loc + "/" + run_dir
        run(run_path)

    return render_template('index.html', data = dir_data)


@app.route("/run", methods = ["GET", "POST"])
def run(run_path):
    if os.path.isdir(run_path) == True:
        file_lists = []
        for f in os.listdir(run_path):
            if f.startswith("pattern"):
                file_lists.append(run_path + "/" + f)

        projects = []
        for fl in file_lists:
            with open(fl) as f:
                for row in f:
                    if row.split(",")[0] not in projects:
                        projects.append(row.split(",")[0])
    else:
        projects = ["a","b","c"]

    return render_template('run.html', run = run_path, proj_data = projects )

index.html:

<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8">
    <link rel="stylesheet" type="text/css" href="{{ url_for('static', filename='style.css') }}">
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.5.1/jquery.min.js"></script>
    <script src="{{url_for('static', filename='asp_javascript.js')}}"></script>
</head>
<body>

    <h2>Packaging Tool - Index</h2>
    <p>Select Run:</p>
    <form action="{{ url_for("index") }}" method="post">
        <select name="run_list">
            {% for d in data %}
            <option>{{d}}</option>
            {% endfor %}
        </select>
        <br>
        <input type="submit" value="submit">
    </form>

</body>
</html>

>Solution :

You can use redirect(...) instead of just calling the function.

from flask import redirect
...
if request.method == "POST":
        run_dir = request.form.get("run_list")
        run_path = dir_loc + "/" + run_dir
        return redirect(url_for('run', run_path=run_path))

@app.route("/run/<run_path>", methods = ["GET", "POST"])
def run(run_path):
    if os.path.isdir(run_path) == True:
    ...
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading