jestembotem
jestembotem

Reputation: 85

Input from another file

I've got 2 python files. This is the first one:

class Downloader():

    def __init__(self):

        baseURL = 'https://example.com'

    def getDownloadLink(self):            

        #linkBase = input("Text: ")

        responseBase = requests.get(linkBase).content
        soupBase = BeautifulSoup(responseBase, 'lxml')

And second python file:

from flask import Flask
from flask import request
from flask import render_template
from firstFile import Downloader

app = Flask(__name__)

@app.route('/')
def my_form():
    return render_template("form.html")

@app.route('/', methods=['POST'])
def my_form_post():

    linkBase = request.form['text']
    #processed_text = text.upper()

    return Downloader().getDownloadLink()

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

It gives me error:

NameError: name 'linkBase' is not defined

Is it possible to connect linkBase from first file with linkBase in second file ?

Upvotes: 0

Views: 47

Answers (2)

ramnes
ramnes

Reputation: 2160

The problem here is that you're trying to access a variable that doesn't exist in the scope of your getDownloadLink function.

One solution would be to add linkBase as an argument:

def getDownloadLink(self, linkBase):
    responseBase = requests.get(linkBase).content
    soupBase = BeautifulSoup(responseBase, 'lxml')

And then modify your route to send the value to the function:

@app.route('/', methods=['POST'])
def my_form_post():
    linkBase = request.form['text']
    return Downloader().getDownloadLink(linkBase)

Upvotes: 1

FamousJameous
FamousJameous

Reputation: 1583

Modify your code to pass the value as an argument:

class Downloader():

    def __init__(self):

        baseURL = 'https://example.com'

    def getDownloadLink(self, linkBase):            

        #linkBase = input("Text: ")

        responseBase = requests.get(linkBase).content
        soupBase = BeautifulSoup(responseBase, 'lxml')

Second file:

from flask import Flask
from flask import request
from flask import render_template
from firstFile import Downloader

app = Flask(__name__)

@app.route('/')
def my_form():
    return render_template("form.html")

@app.route('/', methods=['POST'])
def my_form_post():

    linkBase = request.form['text']
    #processed_text = text.upper()

    return Downloader().getDownloadLink(linkBase)

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

Upvotes: 0

Related Questions