Flask app runs in Flask but not from Python - apache

I'm trying to get a web page to execute a Python script on my Raspberry Pi.
I've installed Flask as described here, Serving Raspberry Pi with Flask, and entered the sample program:
from flask import Flask
app = Flask(__name__)
#app.route("/")
def hello():
return "Hello World!"
if __name__ == "__main__":
app.run(host='0.0.0.0', port=80, debug=True)
but when I run it (sudo python hello-flask.py) I get and error about it know having a application or something.
I tried the same from The flask website
from flask import Flask
app = Flask(__name__)
#app.route("/")
def hello():
return "Hello World!"
and that runs ok, but using a different run method
$ FLASK_APP=hello.py flask run
which doesn't work for the first app.
what am I doing wrong ?
NOTE : I tried to get Apache to execute my Python scripts, but couldn't get that working either. I got it working to where I could use Python to generate HTML, but not execute a Python script to do anything with the GPIO.

Related

How to get remote-debugging-port for a browser launched by undetected-chromedriver?

I want to use an existing Playwright script to drive a Chrome browser launched by undetected_chromedriver. I've tried the following script:
import time
import undetected_chromedriver as uc
if __name__ == '__main__':
options = uc.ChromeOptions()
options.add_argument('--no-first-run --no-service-autorun --password-store=basic --remote-debugging-port=8989')
driver = uc.Chrome(options=options, port=7878)
driver.get('https://www.google.com')
time.sleep(600)
Yet chrome is launched like this --remote-debugging-port=8989 --remote-debugging-host=127.0.0.1 --remote-debugging-port=54481, my port isn't working, but the autogenerated is. How can I get it other than parsing ps aux output from script?

QWebEngineView not working with PyInstaller

I'm getting a "blank canvas" when using PyInstaller to package and application that utilizes QWebEngineView. Any help would be greatly appreciated!
Here's some code to reproduce the problem...
The following code runs fine with python:
import sys
from PyQt5.QtWidgets import QApplication
from PyQt5.QtWebEngineWidgets import QWebEngineView
from PyQt5.QtCore import QUrl
if __name__ == '__main__':
app = QApplication(sys.argv)
view = QWebEngineView()
view.resize(1024, 600)
view.load(QUrl('http://www.google.com'))
view.show()
sys.exit(app.exec_())
but after packaging with pyinstaller toy.py nothing is shown.
The python application shows:
while the pyinstaller application shows:
So this appears to be a bug in pyinstaller that can be patched by creating a specific file for PyQt.
The first clue in debugging this problem is to take note that QtWebEngineProcess.exe starts (as seen in the Task Manager) when we start from python, but it does not start with our application with pyinstaller. Upon inspection a log is written next to the pyinstaller copy of the QtWebEngineProcess.exe that doesn't really give a helpful message, but it does give a clue that the *.exe was attempted to start and crashed.
It appears that adding a qt.conf to dist/<app>/PyQt5/Qt/bin with the contents
[Paths]
Prefix = ..
fixed my problem.

Instantiate Spacy object only once in apache mod_wsgi environment

myapp.py
---- import statements ---
parser = None
app = Flask(__name__)
#app.route('/xxxxxx/yyy')
def markDealStatud():
text = 'matthew honnibal created spacy library'
parsedData = parser(text.decode("utf-8"))
xxxxxxxxxxxxx
xxxxxxxxxxxx
def initSpacy():
global parser
parser = English()
if __name__ == '__main__':
initSpacy()
app.run()​
if __name__ == 'myapp':
initSpacy()
when i run this app in development mode, __main__ will execute and it will instantiate spacy only once and i will use that.
For production we are using apache server mod_wsgi configuration. similarly i want to instantiate it once(myapp) and use the same obj.
In my configuration it is instantiating for each request. Suggest some solution plz.
Environment
Operating System: Linux
Python Version Used: 2.7
Environment Information: apache mod_wsgi deployment

import matplotlib.pyplot crashes on jupyter notebook

I have a jupyter notebook running on a remote cluster to which I have set up an ssh tunnel. Everything was working fine till today. Now everytime I do :
import matplotlib # This works
%matplotlib inline # This causes kernel to restart
import matplotlib.pyplot # This also causes the kernel to restart
Running a standalone ipython interpreter and doing :
import matplotlib
matplotlib.use('agg')
import matplotlib.pyplot ## Leads to Core dumped : Segementation Fault
Running the same on a python interpreter works fine.
Jupyter version : 4.1.1
Python version : 2.7.7
Any help would be much appreciated.
Thank You
Often, this kind of error seems to be related to the backend. Have you tried any other backends? Do these result in the same error? Like this we could narrow down the source of the error.
(I don't have a remote cluster, so I can not reproduce it.)
You can find available backends as described here.
I perhaps have the same problem but on my local machine. I got into jupyter3-qtconsole 4.2.1 with Python 3.4.5 and IPython 5.0.0. and enter
`%matplotlib
Using matplotlib backend: Qt4Agg`
the error message (shortened):
File "/usr/lib64/python3.4/site-packages/tornado/ioloop.py", line 603, in _run_callback
ret = callback()
and finally
from IPython.core.interactiveshell import NoOpContext as context
ImportError: cannot import name 'NoOpContext'
Same thing happens in a notebook but in a straightforward IPython terminal, everything runs OK
Hope this is helpful to someone

Can I switch between wkhtmltopdf libraries programmatically?

I have several versions of wkhtmltopdf libraries installed on my server. I want to be able to switch between them programmatically when I'm about to render them because we have several development teams and they use different versions of wkhtmltopdf. Different wkhtmltopdf version are giving totally different rendered results, which is weird. Is it possible to switch between them programmatically?
This is not a full code but i try to this type of code may it's work for you:
import os
from openerp import tools # this odoo config file master/openerp/tools/which.py
import subprocess
import logging
_logger = logging.getLogger(__name__)
def find_in_path(name):
path = os.environ.get('PATH', os.defpath).split(os.pathsep)
if tools.config.get('bin_path') and tools.config['bin_path'] != 'None':
path.append(tools.config['bin_path'])
return tools.which(name, path=os.pathsep.join(path))
def _get_wkhtmltopdf_bin():
return find_in_path('wkhtmltopdf')
wkhtmltopdf_state = 'install'
try:
process = subprocess.Popen(
[_get_wkhtmltopdf_bin(), '--version'], stdout=subprocess.PIPE, stderr=subprocess.PIPE
)
print "prrrrrrrrrrrrrrrr", process.communicate()[0]
# here write your logic
#
#
#
#
except (OSError, IOError):
_logger.info('You need Wkhtmltopdf to print a pdf version of the reports.')