Tkinter Thread RuntimeError in Selenium Project with Flask - python

I'm working on a Selenium crawling project with Flask, and I'm trying to implement the part that opens the Save As dialog box after crawling is done using filedialog.asksaveasfilename() of the tkinter package.
It works normally when I run it the first time, but from the second time I get an error:
Traceback (most recent call last):
File "C:\Users\yjs01\anaconda3\lib\site-packages\flask\app.py", line 2447, in wsgi_app
response = self.full_dispatch_request()
File "C:\Users\yjs01\anaconda3\lib\site-packages\flask\app.py", line 1952, in full_dispatch_request
rv = self.handle_user_exception(e)
File "C:\Users\yjs01\anaconda3\lib\site-packages\flask\app.py", line 1821, in handle_user_exception
File "C:\Users\yjs01\anaconda3\lib\tkinter\__init__.py", line 3124, in __init__
Widget.__init__(self, master, 'frame', cnf, {}, extra)
File "C:\Users\yjs01\anaconda3\lib\tkinter\__init__.py", line 2572, in __init__
self.tk.call(
RuntimeError: main thread is not in main loop
Also, I don't directly call other GUI windows like root = Tk() but a blank window is created at the same time.
Here is my code:
# main.py
from flask import Flask, render_template, request
from scrap import scrap
from tkinter.filedialog import asksaveasfilename
app = Flask('__name__')
#app.route('/')
def main():
return render_template('main.html')
#app.errorhandler(500)
def page_not_founded(error):
return render_template('error.html')
#app.route('/', methods=['GET', 'POST'])
def execute_scraping():
if request.method == 'POST':
keyword = request.form['keyword']
start_page = int(request.form['start-page'])
end_page = int(request.form['end-page'])
columns = request.form.getlist('column')
file_type = [('엑셀 파일', '*.xlsx')]
file_name = asksaveasfilename(filetypes=file_type, defaultextension=str(file_type))
scrap(keyword, start_page, end_page, columns, file_name)
return render_template('complete.html')
if __name__ == '__main__':
app.run(host='127.0.0.1', port=8081)
I'd appreciate it if you could let me know what the problem is.

Related

Flask app NameError: name 'Markup' is not defined

I have been really stumped on this, been out of this game of web dev/python for a bit. I had a previously working Azure app service running this website, we did a minor HTML spelling change and re deployed. I am assuming some dependency got updated and now its broken. I don't know if this is a packaging version issue or if I have a missing import for my flask app.
I am getting a NameError: name 'Markup' is not defined error when trying to load a static html page. My app starts up just fine but I can't load the actual web pages.
Full Traceback
Traceback (most recent call last):
File "C:\Users\St**\PythonProjects\**Site-Portfolio\lib\site-packages\flask\app.py", line 2095, in __call__
return self.wsgi_app(environ, start_response)
File "C:\Users\St**\PythonProjects\**Site-Portfolio\lib\site-packages\flask\app.py", line 2080, in wsgi_app
response = self.handle_exception(e)
File "C:\Users\St**\PythonProjects\**Site-Portfolio\lib\site-packages\flask_cors\extension.py", line 165, in wrapped_function
return cors_after_request(app.make_response(f(*args, **kwargs)))
File "C:\Users\St**\PythonProjects\**Site-Portfolio\lib\site-packages\flask\app.py", line 2077, in wsgi_app
response = self.full_dispatch_request()
File "C:\Users\St**\PythonProjects\**Site-Portfolio\lib\site-packages\flask\app.py", line 1525, in full_dispatch_request
rv = self.handle_user_exception(e)
File "C:\Users\St**\PythonProjects\**Site-Portfolio\lib\site-packages\flask_cors\extension.py", line 165, in wrapped_function
return cors_after_request(app.make_response(f(*args, **kwargs)))
File "C:\Users\St**\PythonProjects\**Site-Portfolio\lib\site-packages\flask\app.py", line 1523, in full_dispatch_request
rv = self.dispatch_request()
File "C:\Users\St**\PythonProjects\**Site-Portfolio\lib\site-packages\flask\app.py", line 1509, in dispatch_request
return self.ensure_sync(self.view_functions[rule.endpoint])(**req.view_args)
File "C:\Users\St**\PycharmProjects\**Site-portfolio\application.py", line 32, in index
return render_template("//index.html")
File "C:\Users\St**\PythonProjects\**Site-Portfolio\lib\site-packages\flask\templating.py", line 147, in render_template
ctx.app.update_template_context(context)
File "C:\Users\St**\PythonProjects\**Site-Portfolio\lib\site-packages\flask\app.py", line 756, in update_template_context
context.update(func())
File "C:\Users\St**\PythonProjects\**Site-Portfolio\lib\site-packages\flask_recaptcha.py", line 59, in get_code
return dict(recaptcha=Markup(self.get_code()))
NameError: name 'Markup' is not defined
127.0.0.1 - - [08/Apr/2022 17:02:51] "GET /?__debugger__=yes&cmd=resource&f=style.css HTTP/1.1" 304 -
127.0.0.1 - - [08/Apr/2022 17:02:51] "GET /?__debugger__=yes&cmd=resource&f=debugger.js HTTP/1.1" 304 -
127.0.0.1 - - [08/Apr/2022 17:02:51] "GET /?__debugger__=yes&cmd=resource&f=console.png HTTP/1.1" 304 -
Process finished with exit code 0
Here is my code:
from flask import Flask, request, render_template, flash
from flask_mail import Mail, Message
from flask_cors import CORS, cross_origin
from flask_recaptcha import ReCaptcha
import requests
import json
import os
app = Flask(__name__, static_folder='static', static_url_path='')
recaptcha = ReCaptcha(app=app)
app.config['RECAPTCHA_ENABLED'] = True
app.config['RECAPTCHA_PUBLIC_KEY'] = '***********************'
app.config['RECAPTCHA_PRIVATE_KEY'] = '****************************'
#app.route('/', methods=['GET'])
def index():
return render_template("//index.html")
#app.route('/contact-us', methods=['GET'])
#app.route('/contact', methods=['GET', 'POST'])
def contact():
if request.method == 'POST':
r = requests.post('https://www.google.com/recaptcha/api/siteverify',
data={'secret': '***********',
'response': request.form['g-recaptcha-response']})
google_response = json.loads(r.text)
if google_response['success']:
contact_form = {'name': request.form['name'],
'email': request.form['email'],
'message': request.form['message']}
msg = Message(subject='Contact from website',
sender=contact_form['email'],
recipients=['*************'],
body=contact_form['message'])
mail.send(msg)
flash('Success, we will respond within at least 24 hours.')
return render_template('contact.html')
else:
flash('failed to submit, please retry or contact us at ************')
return render_template('contact.html')
return render_template('contact.html')
if __name__ == '__main__':
app.run()
My requirements.txt for package version
Flask>=1.0.2
jinja2>=2.11.3
Flask-Mail>=0.9.1
Flask-Cors>=3.0.9
Flask-Admin>=1.5.2
Flask-ReCaptcha>=0.4.2
Flask-SQLAlchemy>=2.3.2
Flask-WTF>=0.14.2
SQLAlchemy>=1.3.0
requests>=2.20.0
Flask-Login>=0.4.1
Werkzeug>=0.15.3
Flask-ReCaptcha is a very old project. The last update of Flask-ReCaptcha is on 2016. You'd better not use it.
Back to the error log itself, Flask-ReCaptcha has code like from jinja2 import Markup. But, since jinja2==3.1.0, Markup's position has changed. Try pip install jinja2==3.0.0`.
You will probably meet other problems as Flask-ReCaptcha is really old.
Go to Python installation folder
Go to Lib > site-packages
Open flask_recaptcha.py
You'll see something like this:
try:
from flask import request
from jinja2 import Markup
import requests
except ImportError as ex:
print("Missing dependencies")
Change it to:
try:
from flask import request
from markupsafe import Markup
import requests
except ImportError as ex:
print("Missing dependencies")

Possible Python Flask Memory Leak

Hoping someone can help me address this issue.
I've got a very simple Flask server running in a docker container which accepts a new frame every time the /api/feed_image endpoint is called. The image passed through that endpoint then streams to the index.html from /video_feed.
The application works as expected initially, however, when I watch the resources being consumed using docker stats the memory usage slowly climbs until the entire system crashes.
Does anyone know what I could have missed or have any suggestions?
Thanks!
import os
import sys
from azure.iot.device.aio import IoTHubModuleClient
from flask import Response
from flask import request
from flask import Flask
from flask import render_template
import datetime
import time
import numpy as np
import cv2
import queue
frameQueue = queue.Queue()
app = Flask(__name__)
#app.route("/")
def index():
return render_template("index.html")
#app.route("/video_feed")
def video_feed():
print(len(frameQueue))
return Response(generate(), mimetype="multipart/x-mixed-replace; boundary=frame")
#app.route("/api/feed_image", methods=['POST'])
def read_image():
r = request
nparr = np.fromstring(r.data, np.uint8)
frameQueue.put(cv2.imdecode(nparr, cv2.IMREAD_COLOR))
def generate():
global frameQueue
while True:
outputFrame = frameQueue.get()
if outputFrame is None:
continue
(flag, encodedImage) = cv2.imencode(".jpg", outputFrame)
if not flag:
continue
yield(b'--frame\r\n' b'Content-Type: image/jpeg\r\n\r\n' + bytearray(encodedImage) + b'\r\n')
if __name__ == "__main__":
app.run(host='0.0.0.0', port='5001', debug=True, threaded=True, use_reloader=False)
After a long time I solved my own problem.
Turns out because I wasn't returning a Response object at the end of read_image(), Flask was generating an exception, which must have been caching in the background:
172.18.0.4 - - [22/Apr/2021 00:09:25] "POST /api/feed_image HTTP/1.1" 500 -
Traceback (most recent call last):
File "/usr/local/lib/python3.7/site-packages/flask/app.py", line 2464, in __call__
return self.wsgi_app(environ, start_response)
File "/usr/local/lib/python3.7/site-packages/flask/app.py", line 2450, in wsgi_app
response = self.handle_exception(e)
File "/usr/local/lib/python3.7/site-packages/flask/app.py", line 1867, in handle_exception
reraise(exc_type, exc_value, tb)
File "/usr/local/lib/python3.7/site-packages/flask/_compat.py", line 39, in reraise
raise value
File "/usr/local/lib/python3.7/site-packages/flask/app.py", line 2447, in wsgi_app
response = self.full_dispatch_request()
File "/usr/local/lib/python3.7/site-packages/flask/app.py", line 1953, in full_dispatch_request
return self.finalize_request(rv)
File "/usr/local/lib/python3.7/site-packages/flask/app.py", line 1968, in finalize_request
response = self.make_response(rv)
File "/usr/local/lib/python3.7/site-packages/flask/app.py", line 2098, in make_response
"The view function did not return a valid response. The"
TypeError: The view function did not return a valid response. The function either returned None or ended without a return statement.
Adding a return statement solved the problem.
#app.route("/api/feed_image", methods=['POST'])
def read_image():
...
return Response(status=201)
Perhaps someone would have an explanation as to why this would happen?
Either way, I hope this helps someone!

TypeError while running python webapp, what anamoly in python code could have caused it? [duplicate]

This question already has answers here:
Flask view return error "View function did not return a response"
(3 answers)
Closed 2 years ago.
I wrote the framework for a web app using python flask and PostgreSQL as the back end. It should allow one to submit text to be saved. But when I tried running it, I get the following TypeError:
TypeError: The view function did not return a valid response. The
function either returned None or ended without a return statement.
The traceback to the error page:
Traceback (most recent call last) File
"/opt/anaconda3/lib/python3.8/site-packages/flask/app.py", line 2464,
in call return self.wsgi_app(environ, start_response) File
"/opt/anaconda3/lib/python3.8/site-packages/flask/app.py", line 2450,
in wsgi_app response = self.handle_exception(e) File
"/opt/anaconda3/lib/python3.8/site-packages/flask/app.py", line 1867,
in handle_exception reraise(exc_type, exc_value, tb) File
"/opt/anaconda3/lib/python3.8/site-packages/flask/_compat.py", line
39, in reraise raise value File
"/opt/anaconda3/lib/python3.8/site-packages/flask/app.py", line 2447,
in wsgi_app response = self.full_dispatch_request() File
"/opt/anaconda3/lib/python3.8/site-packages/flask/app.py", line 1953,
in full_dispatch_request return self.finalize_request(rv) File
"/opt/anaconda3/lib/python3.8/site-packages/flask/app.py", line 1968,
in finalize_request response = self.make_response(rv) File
"/opt/anaconda3/lib/python3.8/site-packages/flask/app.py", line 2097,
in make_response raise TypeError( TypeError: The view function did not
return a valid response. The function either returned None or ended
without a return statement. The debugger caught an exception in your
WSGI application. You can now look at the traceback which led to the
error. To switch between the interactive traceback and the plaintext
one, you can click on the "Traceback" headline. From the text
traceback you can also create a paste of it. For code execution
mouse-over the frame you want to debug and click on the console icon
on the right side.
You can execute arbitrary Python code in the stack frames and there
are some extra helpers available for introspection:
dump() shows all variables in the frame dump(obj) dumps all that's
known about the object Brought to you by DON'T PANIC, your friendly
Werkzeug powered traceback interpreter.
My code is the following, I looked for a long time without finding any problems:
from flask import Flask, render_template, request
from flask_sqlalchemy import SQLAlchemy
app = Flask(__name__)
ENV = 'dev'
if ENV == 'dev':
app.debug = True
app.config['SQLALCHEMY_DATABASE_URI'] = 'postgresql://postgres:Gray#localhost/Phict'
app.config['SQLALCHEMY_TRACK_MODIFICATIONS'] = False
db = SQLAlchemy(app)
class Feedback(db.Model):
__tablename__ = 'feedback'
id = db.Column(db.Integer, primary_key=True)
comments = db.Column(db.Text())
def __init__(self, comments):
self.comments = comments
#app.route('/')
def index():
return render_template('index.html')
#app.route('/submit', methods=['POST'])
def submit():
if request.method == 'POST':
comments = request.form['fileToUpload']
data = Feedback(comments)
db.session.add(data)
db.session.commit()
if __name__ == '__main__':
app.run()
as stated by #snakecharmerb in his comment above
The submit function doesn't return anything. It must return something, even if it's just an empty string.
so in your case, you may need to return redirection object to the home page for e.g
#app.route('/submit', methods=['POST'])
def submit():
if request.method == 'POST':
comments = request.form['fileToUpload']
data = Feedback(comments)
db.session.add(data)
db.session.commit()
# you can add a 'flash' message and then redirect to the index page
return redirect(url_for('index')) # -- HERE --
refer to this topic https://flask.palletsprojects.com/en/1.1.x/api/#flask.redirect

RuntimeError: main thread is not in main loop from Flask calling Tkinter

I'm setting up a Facebook messenger bot which could execute code remotely on my laptop (i.e taking screenshot, display messages). When I send the message "msg hello" from messenger for the bot to display a message box with "hello" using Tkinter.
The program returns with the error:
RuntimeError: main thread is not in main loop
I tried run Tkinter with:
root = tkinter.Tk()
root.mainloop()
but the program just stay freeze there (like a while True loop)
here is the code of my main flask app:
from flask import Flask, request
from pymessenger.bot import Bot
from functions.message_box import msg_box
app = Flask(__name__)
ACCESS_TOKEN = 'my token' # I removed these tokens on purpose
VERIFY_TOKEN = 'my token'
bot = Bot(ACCESS_TOKEN)
#app.route("/", methods=['GET', 'POST'])
def receive_message():
if request.method == 'GET':
# Assuming the request is from facebook's verfication
token_sent = request.args.get("hub.verify_token")
return verify_fb_token(token_sent)
else:
# get whatever message a user sent the bot
output = request.get_json()
for event in output['entry']:
messaging = event['messaging']
for message in messaging:
if message.get('message'):
#Facebook Messenger ID for user so we know where to send response back to
recipient_id = message['sender']['id']
# get code from the message
text = message['message'].get('text')
code = text[:text.find(' ')] # find the first word in the message
if code == 'msg':
msg = text[text.find(' ') + 1:] # get the message except the code
msg_box(msg)
send_message(recipient_id, f'Sent: {msg}')
return "Message Processed"
def verify_fb_token(token_sent):
if token_sent == VERIFY_TOKEN:
return request.args.get("hub.challenge")
return 'Invalid verification token'
#uses PyMessenger to send response to user
def send_message(recipient_id, response):
#sends user the text message provided via input response parameter
bot.send_text_message(recipient_id, response)
return "success"
if __name__ == "__main__":
app.run()
here is my code from message_box.py (to display message box on window):
'''
Purpose: Display msg box on screen
'''
import tkinter
from tkinter import messagebox
# hide main window
root = tkinter.Tk()
# root.mainloop()
root.withdraw()
def msg_box(msg):
messagebox.showinfo("Information", msg)
return 1
Full Error I received:
* Serving Flask app "app" (lazy loading)
* Environment: production
WARNING: This is a development server. Do not use it in a production deployment.
Use a production WSGI server instead.
* Debug mode: off
* Running on http://127.0.0.1:5000/ (Press CTRL+C to quit)
[2019-09-05 15:52:47,663] ERROR in app: Exception on / [POST]
Traceback (most recent call last):
File "C:\Users\kevin\AppData\Local\Programs\Python\Python37-32\lib\site-packages\flask\app.py", line 2446, in wsgi_app
response = self.full_dispatch_request()
File "C:\Users\kevin\AppData\Local\Programs\Python\Python37-32\lib\site-packages\flask\app.py", line 1951, in full_dispatch_request
rv = self.handle_user_exception(e)
File "C:\Users\kevin\AppData\Local\Programs\Python\Python37-32\lib\site-packages\flask\app.py", line 1820, in handle_user_exception
reraise(exc_type, exc_value, tb)
File "C:\Users\kevin\AppData\Local\Programs\Python\Python37-32\lib\site-packages\flask\_compat.py", line 39, in reraise
raise value
File "C:\Users\kevin\AppData\Local\Programs\Python\Python37-32\lib\site-packages\flask\app.py", line 1949, in full_dispatch_request
rv = self.dispatch_request()
File "C:\Users\kevin\AppData\Local\Programs\Python\Python37-32\lib\site-packages\flask\app.py", line 1935, in dispatch_request
return self.view_functions[rule.endpoint](**req.view_args)
File "app.py", line 59, in receive_message
msg_box(msg)
File "C:\Users\kevin\Desktop\python\laptopRemoteController\functions\message_box.py", line 13, in msg_box
messagebox.showinfo("Information", msg)
File "C:\Users\kevin\AppData\Local\Programs\Python\Python37-32\lib\tkinter\messagebox.py", line 83, in showinfo
return _show(title, message, INFO, OK, **options)
File "C:\Users\kevin\AppData\Local\Programs\Python\Python37-32\lib\tkinter\messagebox.py", line 72, in _show
res = Message(**options).show()
File "C:\Users\kevin\AppData\Local\Programs\Python\Python37-32\lib\tkinter\commondialog.py", line 39, in show
w = Frame(self.master)
File "C:\Users\kevin\AppData\Local\Programs\Python\Python37-32\lib\tkinter\__init__.py", line 2744, in __init__
Widget.__init__(self, master, 'frame', cnf, {}, extra)
File "C:\Users\kevin\AppData\Local\Programs\Python\Python37-32\lib\tkinter\__init__.py", line 2299, in __init__
(widgetName, self._w) + extra + self._options(cnf))
RuntimeError: main thread is not in main loop
127.0.0.1 - - [05/Sep/2019 15:52:47] "POST / HTTP/1.1" 500 -
[2019-09-05 15:52:49,220] ERROR in app: Exception on / [POST]
Traceback (most recent call last):
File "C:\Users\kevin\AppData\Local\Programs\Python\Python37-32\lib\site-packages\flask\app.py", line 2446, in wsgi_app
response = self.full_dispatch_request()
File "C:\Users\kevin\AppData\Local\Programs\Python\Python37-32\lib\site-packages\flask\app.py", line 1951, in full_dispatch_request
rv = self.handle_user_exception(e)
File "C:\Users\kevin\AppData\Local\Programs\Python\Python37-32\lib\site-packages\flask\app.py", line 1820, in handle_user_exception
reraise(exc_type, exc_value, tb)
File "C:\Users\kevin\AppData\Local\Programs\Python\Python37-32\lib\site-packages\flask\_compat.py", line 39, in reraise
If there is a mistake please help me correct them. Moreover, if there is another approach please show me how.
Thank you for your time and patience.

AttributeError when using pandas and Google App Engine

I understand that Pandas isn't included and believe that I've properly included the library via adding the module into the lib directory for the app and by adding
from google.appengine.ext import vendor
vendor.add('lib')
to appengine_config.py - other modules don't seem to be having issues.
When I run my app, the following stack trace shows up:
ERROR 2016-12-15 23:05:31,038 app.py:1587] Exception on / [GET]
Traceback (most recent call last): File
".../PycharmProjects/fpl-flask-app/lib/flask/app.py", line 1988, in
wsgi_app  response = self.full_dispatch_request() File
".../PycharmProjects/fpl-flask-app/lib/flask/app.py", line 1641, in
full_dispatch_request
  rv = self.handle_user_exception(e) File ".../PycharmProjects/fpl-flask-app/lib/flask/app.py", line 1544, in
handle_user_exception
  reraise(exc_type, exc_value, tb) File ".../PycharmProjects/fpl-flask-app/lib/flask/app.py", line 1639, in
full_dispatch_request
  rv = self.dispatch_request() File ".../PycharmProjects/fpl-flask-app/lib/flask/app.py", line 1625, in
dispatch_request
  return self.view_functionsrule.endpoint File ".../PycharmProjects/fpl-flask-app/main.py", line 13, in index
  datar = pandas.read_sql('SELECT p.nationality, SUM(s.mins_played) AS mins_played   FROM CurrentSeasonStats s left join Player p ON
s.Player_pid = p.pid GROUP BY   p.nationality', con)
AttributeError: 'module' object has no attribute 'read_sql'**
Here's my code:
from flask import Flask, request, render_template
import pandas
from sqlalchemy import create_engine
import pymysql
import random
app = Flask(__name__)
#app.route('/')
def index():
con = create_engine('mysql+pymysql://user:pass#ix.cs.uoregon.edu:port/fpl', echo=False)
datar = pandas.read_sql('SELECT p.nationality, SUM(s.mins_played) AS mins_played FROM CurrentSeasonStats s left join Player p ON s.Player_pid = p.pid GROUP BY p.nationality', con)
return render_template('index.html', table=datar)
if __name__ == '__main__':
app.run()
Any ideas?
I had copy-pasted a Binary Skeleton version of the pandas library into /lib/ which is why none of the pandas functions were working.
This a direct and annoying result of me not using virtual environments...
USE VIRTUAL ENVIRONMENTS, KIDS!

Categories