0

I am facing a little problem when building my flask application. In my UI, I have added a couple of free field where I am supposed to put the url to a couple of files hosted on my machine (the app is deployed from my computer).

Basically those two urls are usually links to a csv and a txt file that I used to run my my module

In my python module it was written like this

csv_client = "requirements.csv"
txt_provider = "textexample.txt"

In my flask application, I written the whole thing like this

app = Flask(__name__)


def master(input_client, input_provider):
    
    messages = []
    messages.append('Master function has been launched.')
    
    csv_client = input_client
    txt_provider = input_provider
    
    #rest of my function...
    
    return df_matched


@app.route('/', methods=['GET', 'POST'])
def index():
    messages = []
    output_file_url = ""
    
    if request.method == 'POST':
        input_client = request.form.get('inputClient')
        input_provider = request.form.get('inputProvider')
        
        df_matched, messages = master(input_client, input_provider)
        output_file_url = "/path/to/your/Final_GDPR_ANALYSIS.csv"  # update this with the actual path

    return render_template('index.html', messages=messages, output_file_url=output_file_url)

if __name__ == '__main__':
    app.run(debug=True)


And in case it matters, here is my html / css template

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Your Flask App</title>
    <!-- Include Bootstrap CSS for basic styling. You can customise this. -->
    <link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.3.1/css/bootstrap.min.css">
</head>
<body>
    <div class="container">
        <h1 class="mt-5">Your Flask App</h1>

        <!-- Form to accept URLs and submit to Flask application. -->
        <form method="post" action="/run-master-function">
            <div class="form-group">
                <label for="inputClient">Input Client</label>
                <input type="text" class="form-control" id="inputClient" name="inputClient" placeholder="Enter URL for client file">
            </div>
            <div class="form-group">
                <label for="inputProvider">Input Provider</label>
                <input type="text" class="form-control" id="inputProvider" name="inputProvider" placeholder="Enter URL for provider file">
            </div>
            <button type="submit" class="btn btn-primary">Run</button>
        </form>
        
        <!-- Conditional block to display messages and download link. -->
        {% if messages %}
            <h3>Messages:</h3>
            <ul>
                {% for message in messages %}
                    <li>{{ message }}</li>
                {% endfor %}
            </ul>
        {% endif %}
        
        {% if output_file_url %}
            <a href="{{ output_file_url }}" download>Download Output</a>
        {% endif %}
    </div>
</body>
</html>


For some reasons, I keep getting a url message error when adding my two url in my app. I tried a few syntax, checked whether it was a permission matter but did not find anything conclusive. I also tried to host the files on a google drive to use the url instead, but nothing conclusive.

C:\Users\rapha\Desktop\Consulting\Compliance\requirements.csv
"C:\Users\rapha\Desktop\Consulting\Compliance\requirements.csv"
C:\\Users\\rapha\\Desktop\\Consulting\\Compliance\\requirements.csv
"C:\\Users\\rapha\\Desktop\\Consulting\\Compliance\\requirements.csv"

Would you have any suggestion on how to fix such matter here?

Cheers

Raphaël Ambit
  • 149
  • 1
  • 1
  • 7

1 Answers1

0

Maybe this can help, quick and dirty example.

run.py

from flask import Flask, render_template, request, url_for, flash, redirect
app = Flask(__name__)


def master(input_client, input_provider):

    messages = []
    messages.append('Master function has been launched.')

    csv_client = input_client
    txt_provider = input_provider

    # rest of my function...
    messages.append(csv_client)
    messages.append(txt_provider)
    df_matched = messages
    return df_matched


@app.route('/', methods=['GET', 'POST'])
def index():
    messages = []
    output_file_url = ""

    if request.method == 'POST':
        input_client = request.form.get('inputClient')
        input_provider = request.form.get('inputProvider')

        messages = master(input_client, input_provider)
        output_file_url = "Final_GDPR_ANALYSIS.txt"  # update this with the 
        actual path
        return render_template('result.html', messages=messages, 
            output_file_url=output_file_url)
    else:
        return render_template('index.html', messages=messages, 
            output_file_url=output_file_url)


if __name__ == '__main__':
    app.run(debug=True)

index.html

<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <title>Your Flask App</title>
    <!-- Include Bootstrap CSS for basic styling. You can customise this. -->
    <link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.3.1/css/bootstrap.min.css">
</head>

<body>
    <div class="container">
        <h1 class="mt-5">Your Flask App</h1>
        <li><a href="/">Home</a></li>

        <!-- Form to accept URLs and submit to Flask application. -->
        <form method="post" action="{{ request.path }}">
            <div class="form-group">
                <label for="inputClient">Input Client</label>
                <input type="text" class="form-control" id="inputClient" name="inputClient"
                    placeholder="Enter URL for client file">
            </div>
            <div class="form-group">
                <label for="inputProvider">Input Provider</label>
                <input type="text" class="form-control" id="inputProvider" name="inputProvider"
                    placeholder="Enter URL for provider file">
            </div>
            <button type="submit" class="btn btn-primary">Run</button>
        </form>

        <!-- Conditional block to display messages and download link. -->
        {% if messages %}
        <h3>Messages:</h3>
        <ul>
            {% for message in messages %}
            <li>{{ message }}</li>
            {% endfor %}
        </ul>
        {% endif %}

        {% if output_file_url %}
        <a href="{{ output_file_url }}" download>Download Output</a>
        {% endif %}
    </div>
</body>

</html>

result.html

<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <title>Your Flask App</title>
    <!-- Include Bootstrap CSS for basic styling. You can customise this. -->
    <link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.3.1/css/bootstrap.min.css">
</head>

<body>
    <div class="container">
        <h1 class="mt-5">Your Flask App</h1>
        <li><a href="/">Home</a></li>

        <!-- Conditional block to display messages and download link. -->
        {% if messages %}
        <h3>Messages:</h3>
        <ul>
            {% for message in messages %}
            <li>{{ message }}</li>
            {% endfor %}
        </ul>
        {% endif %}

        {% if output_file_url %}
        <a href="{{ output_file_url }}" download>Download Output</a>
        {% endif %}
    </div>
</body>

</html>

And working result:

https://github.com/spawnmarvel/test/blob/main/url.jpg

And the file stuff

Flask Download a File

aspen l
  • 54
  • 7