Jump to content

[Answered] Best practices in debugging flask/django and requesting python modules


mabass

Recommended Posts

Hi,

 

figured it's better to ask here before going straight to customer service. If I'm mistaken and both questions are better directed towards them let me know.

 

i'm currently trying to migrate my flask sandbox (intended to provide python scripts to others as simple web apps) to HelioHost and a few questions have cropped up.

Fyi, I'm decent enough in Python (though not a computer scientist by profession) but woefully inexperienced when it comes to server and web administration.

 

1. Is there a decent way to debug the online-version of flask without relying too heavily on the CRON-jobs?

Setting the debug-flag in app.run() to True does not seem to work, I assume it's globally disabled?

 

2. What's the proper way to request python modules? My flask app is currently missing at least one specific module to run properly.

 

 

Thanks!

 

 

PS:

The Flask-Tutorial on the wiki (https://wiki.helionet.org/tutorials/flask) has an erroneous ";" in the flask.wsgi code, line 4.

Link to comment
Share on other sites

1. Is there a decent way to debug the online-version of flask without relying too heavily on the CRON-jobs?

Not really. Most of the log files are full of information from all of the users on the server, so we can't provide direct access due to the possibility that the logs contain private information from another user. There is the error log button in cpanel that you may not have seen yet. Sometimes it contains useful information, but most of the time an admin can get a more detailed error message. Cron jobs can usually do a pretty good job of emailing you the errors, but you can only use them twice a day so that limits the usefulness. If you want us to try to get you a better error message just post the URL to the flask app.

 

Setting the debug-flag in app.run() to True does not seem to work, I assume it's globally disabled?

Probably. Most of the time in a production environment you want potentially malicious visitors to your site to not see the error messages, because they could use them against you to find vulnerabilities in your code. PHP ships with error messages disabled by default for this reason, and it wouldn't surprise me if flask was the same way.

 

2. What's the proper way to request python modules? My flask app is currently missing at least one specific module to run properly.

Make a post in customer service, and include your username, server, and python version that you're using. You can just reply to this thread if you want rather than making a new post. Not a big deal.

 

The Flask-Tutorial on the wiki (https://wiki.helionet.org/tutorials/flask) has an erroneous ";" in the flask.wsgi code, line 4.

Wow, you're right. That semicolon has probably been there for 4 years or more. It works with it there and it works without it there so erroneous might be too strong of a word since it doesn't cause an error. I would agree with the word unneeded though. I edited the wiki. Thanks for pointing that out.
Link to comment
Share on other sites

Thanks. I did indeed miss the error log. The application fails at this import statement:

from flask_wtf import FlaskForm

Flask-wtf on the server is 0.14.2, which tries to import a deprecated werkzeug-function. This has been fixed in 0.14.3

As per modules:
As far as I can tell I only need pdfrw==0.4.
idna==2.9 isn't on module-list but should be a default python module iirc.

A few more modules on the server are out of date, though I have yet to see if they break anything.

  • beautifulsoup4
  • flask
  • flask-wtf
  • jinja2
  • soupsieve
  • urllib3
  • wtforms

 

Wow, you're right. That semicolon has probably been there for 4 years or more. It works with it there and it works without it there so erroneous might be too strong of a word since it doesn't cause an error. I would agree with the word unneeded though. I edited the wiki. Thanks for pointing that out.

Heh, I forgot how lenient Python actually is. I was wondering why no one had complained about their flask app not working despite following the tutorial.

Edited by mabass
Link to comment
Share on other sites

Awesome. Seems to work fine.

 

I'm now running into pathing issue, probably due my inexperience with .wsgi and server/web admin in general:

 

One app dynamically creates img src links, the images are within the /static/ folder hierarchy.

The paths are created here https://github.com/Wolfenswan/flask-sandbox/blob/master/projects/adx_abctrainer/adx_abctrainer.py#L10

 

As the tags are injected into existing html I can't use Jinja templates.

Locally and on pythonanywhere I can provide the relative path to the static folder with '/static/adx_abctrainer/img/'.
However on HelioHost this 404s, as it assumes 'https://mabass.heliohost.org' as root rather than 'https://mabass.heliohost.org/flask'/.

Using '../static/adx_abctrainer/img/' is a functional workaround, but I feel I'm probably missing a best practice.
 

Edited by mabass
Link to comment
Share on other sites

If you start a url with a / it means start at the webroot, which is /home/mabass/public_html/ for that domain. If you run your app on a subdomain instead of a subdirectory you might get the results you're expecting. For instance if you created a subdomain called flask.mabass.heliohost.org then the webroot would be /home/mabass/public_html/flask/ and if you used a url that started with a / it would start at the webroot which seems to be what you're expecting. Also /static/adx_abctrainer/img/ is an absolute url path since it starts with a /. A relative path would start with a . (period) .. (two periods) or a directory name.

Link to comment
Share on other sites

Thanks, that helped a lot.

 

After resolving a few more pathing issues everything seems to work as intended now

 

Regarding git/version-control:
Is it correct that I can only pull from an existing repository but neither commit/drop local changes? I couldn't find a command line access or the relevant backend-controls in cpanel

Edited by mabass
Link to comment
Share on other sites

I figured, thanks.

 

Another question if you don't mind:
I tried adding an addon domain that directly points towards public_html/mabass.tk/ (identical with /flask/ for now), however, this now produces a 500 error due to (I assume) an issue in either the .htaccess or .wsgi.

Any pointers what I need to look at? https://mabass.heliohost.org/mabass.tk/ works fine.

Link to comment
Share on other sites

Not sure why, but I struggle at grasping mod_rewrite. I understand the general syntax and regex well enough, but the overall logic or structure eludes me.

 

More precisely:
I mirrored my flask-instance to into a dev- and live-/production-instance. Both work fine when accessing them from mabass.heliohost.org.

 

However, the sub-domains I created for testing purposes all produce a 500:
flask-dev.mabass.heliohost.org - to /flask-dev/

tk.mabass.heliohost.org (for the mabass.tk addon domain) - to /flask-live/

 

As I understand it, I have to add a RewriteCond to .htaccess, so I added

RewriteCond %{HTTP_HOST} ^flask-dev\.mabass\.heliohost\.org$ [NC]

producing:

RewriteEngine On
RewriteBase /
RewriteRule ^(media/.*)$ - [L]
RewriteRule ^(admin_media/.*)$ - [L]
RewriteRule ^(flask\.wsgi/.*)$ - [L]
RewriteRule ^(.*)$ flask-dev/flask.wsgi/$1 [QSA,PT,L]

RewriteCond %{HTTP_HOST} ^flask-dev\.mabass\.heliohost\.org$ [NC]
RewriteRule ^(flask\.wsgi/.*)$ - [L]
RewriteRule ^(.*)$ flask-dev/flask.wsgi/$1 [QSA,PT,L]

Still, no luck.
 

 

 


 

 

Link to comment
Share on other sites

That works, thanks.

 

Just so I get this right:
My problem wasn't with the condition but that the subdomain was trying to route through a directory it couldn't find?

And if, theoretically, I wanted it to work both with https://mabass.heliohost.org/flask-dev/ (which is now broken) and the subdomain I'd have to split the current RewriteRule into two more specific ones, one catering for each?

 

 

Link to comment
Share on other sites

Correct. You were getting a 500 error because when you access flask-dev.mabass.heliohost.org your webroot is /home/mabass/public_html/flask-dev/ and since the .htaccess was redirecting with this line RewriteRule ^(.*)$ flask-dev/flask.wsgi/$1 [QSA,PT,L] it was looking for /home/mabass/public_html/flask-dev/flask-dev/flask.wsgi which didn't exist.

Link to comment
Share on other sites

  • 2 weeks later...

Thanks!
 

With some more tinkering I got it to work as intended.

Just leaving my ht-access here, in case people google a similar issue  and are looking for pointers:

RewriteCond %{HTTP_HOST} ^mabass\.heliohost\.org$ [NC]
RewriteRule ^(.*)$ flask-live/flask.wsgi/$1 [QSA,PT,L]

RewriteCond %{HTTP_HOST} ^mabass\.tk$ [NC,OR]
RewriteCond %{HTTP_HOST} ^mabass\.de$ [NC]
RewriteRule ^(.*)$ flask.wsgi/$1 [QSA,PT,L]

 

Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...