2

I have a flask web application which has two methods. I need to access the summary variable which is in method 1 inside method 2. the following is what I have done. But it doesn't seem to work out for me.

Method 1

app = Flask(__name__)
@app.route('/templates', methods=['POST'])
def original_text_form():
    title = "Summarizer"
    text = request.form['input_text']  # Get text
    max_value = sent_tokenize(text)
    num_sent = int(request.form['num_sentences'])  # Get number of sentence required in summary
    sum1 = summarize()
    summary = sum1.get_summary(text, num_sent)
    print(summary)


    return render_template("index.html", title = title, original_text = text, output_summary = summary, num_sentences = max_value)

Method 2

@app.route('/savetextfile', methods=['POST'])

def saveToFile():
    x = original_text_form

    with open('/Users/johnsriskandarajah/Documents/summarizer-master/summary.txt', 'wb') as filehandle:
        filehandle.write(x.summary)



    return render_template("index.html", My_Function=saveToFile)
3
  • I'm not clear on what this is supposed to do but perhaps you want to look into sessions. Commented May 21, 2018 at 18:56
  • summarize() is quite unclear? are you trying to setup a file cache to exchange data between functions? why not a database model? Commented May 21, 2018 at 19:05
  • @roganjosh The variable summary in method 1 contains some text that needs to be written to a file. The file writing method is in method 2. So I need to access the variable summary and write it to the file, which I have tried in filehandle.write(x.summary). Commented May 21, 2018 at 19:05

1 Answer 1

2

Why not use a class to group similar functions and allow access to otherwise locally-scoped variables?

app = Flask(__name__)    

class Foo():
    def __init__(self):
        // do something when initialised

    app.route('/method1')
    def method1(self):
        self.summary = something

    app.route('/method2)
    def method2(self):
        function(self.summary)
Sign up to request clarification or add additional context in comments.

1 Comment

No problem! Glad I could be of assistance

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.