Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to do a normal functions in python flask without a route and return the results of this process using a route?

Tags:

python

flask

The app is loading the "links_submit.html" which has a field where you can write a link, like for example (www.google.com) and you submit it, then the app is receiving this URL as HTTP Post and redirecting to another page "post_response.html" which contains a simple html for feedback with the word "Ok". Then I want to do a process with this link (crawl google and search for a specific thing) and after finish this process, automatically redirect from the "post_reponse.html" to another page to show the results I have extracted from google. Now I'm not sure how say to my app on flask: "Ok now lets use a normal function (not route) like for example:

def loadpage(link_sent_by_the_http post):
   res = requests.get('www.google.com')

Imagine that after load the page I also extract some html tag on google and after finish this process I want to redirect the page "post_respose.html" with the "ok" to a new html page which contains the html tag extracted from google. Please note I know how to load the page google and extract what I want, but I don't know how to insert this function/process in the middle of Flask and then redirect from a normal html with "ok" for a new route with the results I have extracted.

import requests
from flask import Flask, render_template, request, url_for

app = Flask(__name__)

@app.route('/test')
def form():
   return render_template('links_submit.html')

@app.route('/links/', methods=['POST'])
def links():
    links=request.form['links']
    return render_template('post_response.html')

Intern Process (Load the received link > Extract what I want)
and then redirect the "post_response.html" to another "html" which will
contain the results that I have extracted) 

if __name__ == '__main__':
  app.run(debug=True)
like image 965
Pablo Avatar asked Oct 18 '22 10:10

Pablo


People also ask

How do you create a function in a Flask?

Flask – Application Flask constructor takes the name of current module (__name__) as argument. The route() function of the Flask class is a decorator, which tells the application which URL should call the associated function. The rule parameter represents URL binding with the function.

What is app route () in Python?

App Routing means mapping the URLs to a specific function that will handle the logic for that URL.

How do you make a response in Flask?

Flask provides a method called make_response() that we can use to send custom headers, as well as change the property (like status_code , mimetype , etc.) in response. We can import make_response from the flask . make_response() accepts a string as a parameter, then creates and returns a response object.


1 Answers

Two ways to do it-

Create a python file say webfunctions.py and put your function in this file. e.g. -

def inc(x):
    return int(x) + 1

Now in your flask app file, you can import the whole file or just the function-

from webfunctions import inc

@app.route('/whatsnext/', methods=['POST'])
def waiting():
    curVal=request.form['x']
    nextVal = inc(curVal)
    return render_template('post_response.html', nextVal=nextVal)

or else, you may declare your definitions at the top of your flask app file. Like below -

import requests
from flask import Flask, render_template, request, url_for

def inc(x):
    return int(x) + 1


@app.route('/whatsnext/', methods=['POST'])
def waiting():
    curVal=request.form['x']
    nextVal = inc(curVal)
    return render_template('post_response.html', nextVal=nextVal)
like image 92
Ejaz Avatar answered Oct 22 '22 09:10

Ejaz