Python and CGI: Display errors in browser instead of downloading file - python

I'm practicing with using Python 3 and CGI to display pages in the browser. I'm running into an issue that I can't seem to figure out though.
There is an error somewhere in my python code I assume but I don't know of a way to figure out what it is.
Basically when I try to run the script the browser wants to download the python file instead. When I comment out a block of code that I suspect is causing the error, it display everything fine.
I have cgitb.enable() imported and included in the file but that's not displaying anything for this error.
Is there a way to see any python errors in the browser? Or at least be able to log the error to a text file that I can go and look at?

I suggest to use Sentry - https://www.getsentry.com/welcome/
It will give you the great interface for errors. You can use it for free.
Note: I am not affiliated with them, I just use that happily for our own solutions.

Related

Fix PyCharm Incorrect Error Inspection (Updating or Temporarily Disabling Code Inspector)

I've recently upgraded my Python from 3.9.x to 3.10.4; I needed it to use match-case statements. I am working on PyCharm, and the code inspection tool has, probably because of the match statements, thrown twenty errors on code that should run without error (I used this code to test the code inspector).
Here's a picture of the problem:
Is there any way to either update the code inspector to prevent it from incorrectly throwing errors (my preferred solution), or disable the code inspector for a segment of the code (I would still like to use the code inspector elsewhere besides where the errors are thrown)?
Thanks in advance.

pdf2image not working with gunicorn/flask

For the past day I have been trying to incorporate pdf2image into my flask webservice.
The problem is, whatever I do, I still get the "Is poppler installed and in PATH?" error message.
I have written a little program that just prints the number of pages in a pdf file. This works completely fine, inside and outside the venv containing flask. If I try to do this from the flask file, I get the error. I even tried calling the very same function in the test code, but to no avail. I don't really know what to do anymore, so any help is greatly appreciated.
P.S.: I don't really know what source to include for this post, so just feel free to ask and I will provide it.
I had the symptoms which you describe. For me the problem was the following:
in the .service file used by systemd to start gunicorn, I had an incomplete path variable. The solution was to add the path to pdftoppm and pdftocairo, which in my case (on Debian 10) was /usr/bin
The solution is therefore that the .service file includes
[Service]
...
Environment="PATH=<your_app_path>/venv/bin:/usr/bin"
Note: This problem and its solution are thus broader than a pdf2image issue, but it seems to show up frequently with pdf2image... assuming this is true, I hope that your question and my response are both relevant.

How can you run python with a bookmark?

I am writing a script to log into web pages (I know it's a bad idea, and I don't care), but I need to run it with a bookmark from Firefox. Does anyone know how to help me?
There is no pre-made solution and as it stands, you left part of your question ambiguous.
But here is one possible outline on how you could accomplish what you want from completely inside firefox.
First off you need to get python in the browser, but it is normally compiled into machine code. The solution is the Transcrypt project, which is Python is transcompiled into javascript and that script is then loaded into your browser. This allows you to run python code.
Extensions to python are made so it can access the DOM in a similiar way you can from javascript.
The transcrypt website has some examples, and some more can be found at a draft page at Mozilla.
Your bookmarklet would be to load a script to load transcrypt and feed it your python code, something like
javascript:(function(){var el=document.createElement('script');el.src='file:///something/something/transcript_loader_and_my_python_code.js';document.body.appendChild(el);})();
But the exact bookmarklet would depend on how you go about things. Writing transcript_loader_and_my_python_code.js is on your own, but if you get the examples from the links above working it should be trivial.

'xoff' not working when using python script to run 'xoff' on linux. Any suggestions?

I am accessing a linux server (telnet) and then running commands on it using python.
I am meant to automate some processes using python including one in which many errors appear and I must type xoff to stop the errors from appearing so I can continue entering more commands.
This works when I do it manually, although when I do it through python, the command just doesn't work. I use pexpect module and use this code: child.sendline("xoff"). Don't worry, child is defined already.
Next I type child.expect(".*") and then print(child.after) which prints the output, which still is the errors that were meant to have stopped by using xoff.
Any suggestions on how to fix this?
BTW I am using Python 2.7.8
For ANY questions you ask, or any more information you need feel free to ask me and I will reply as quick as possible.
Thanks in advance.

Execute a python script from firefox, on a very specific app, just to let firefox provide the graphical IDE

So, I've been searching the topic for quite a while, but I am unable to find out whether it is possible or not. I suspect not, but I need to be sure.
What I'm doing:
I am building a fancy web app that uses xml, xsl and javascript. However, I need to do some transformation to a text file to convert it into a proper xml file. To do this, I use a python script.
What I need:
I am working to make as easy as possible the use of the app, but this gets kind of destroyed when the user has to execute a terminal script before anything. I need a graphical IDE for python, but I just don't have time to learn how to make one, and I would love that the entire app would work within the same web environment, much more intuitive for the user. Then, a really easy way to do that would be to use Firefox as the IDE, for example choosing the file to be transformed through it, and launching from there the python script.
What I require:
I need compatibility only with Firefox. But of course any system capable to install Python and Firefox should work as well. The user would happily install both programs, as long as they have interest in the app.
What I don't know:
Is that possible, to execute an external python script? If not, for security reasons I imagine, is there any way for the user to allow it? Maybe through the installation of a plugin?
maybe http://mozex.mozdev.org/ is what I am looking for?
Thanks!
Gerard
Yes, It is possible to execute an external python script. You can use a simpleServer (example) that receives a (Ajax) request from your page and calls a subprocess or os.startfile to launch the script. In fact, you can call the function itself from within the server and return the result (e.g. as json) to the page and use your javascript to handle it.
If you're after actually using python in firefox, rather than having the server side written in python (although the webserver can of course be running locally), I would guess pythonext (used to be pyxpcomext) is what you need.

Categories

Resources