Error with flask frozen module for my website - python

So I've been trying to deploy my flask website to the public, but it's been quite annoying to say the least. I'm finished with my website and it runs on the localhost. However, when I try to use the frozen-flask module, as suggested by a tutorial I saw on Medium.com, to "freeze" my html files into static files so I can have a static website (I made a personal website), I get an error in my freeze.py file. Here's the code for it:
from flask_frozen import Freezer
#main.py is the python file where I created my app
from main import app
freezer = Freezer(app)
if __name__ == '__main__':
#This is the line that appears to be giving me the error when I run the code from the terminal.
freezer.freeze()
That Medium.com tutorial only used 1 html file and no css (just a basic hello world application), so I have no clue if that would change anything, as I have several html files and a main.css file. Also, when I run "python freeze.py" in the terminal, it creates a directory in my project named "build" (as expected) but underneath the directory, it creates several other directories, named after all my html files. Underneath those directories, it has a file called "index.html" (for ALL of the directories). The files look something like this because I know that explanation wasn't too good. For some reason, this didn't happen when I initially used flask-frozen, it just created a bunch of files named after my html files, with no unnecessary extra directories.
- Name of project
- build
- name of one of my html files
- index.html
- name of one of my html files
- index.html
etc...
If anyone knows why this happens, or if I even need to frozen-flask to deploy my personal website (please, god, let there be a better approach), it would be greatly appreciated if I could know. I'm trying to use Netlify if that helps at all (seems to be much more straight forward than other platforms like Heroku).

You do not need a frozen flask to deploy website, a basic flask app will do. Just make sure to have the correct port to run on.
I recommend pretty-printed's tutorials on flask
https://www.youtube.com/watch?v=CjYKrbq8BCw&list=PLXmMXHVSvS-CoYS177-UvMAQYRfL3fBtX
For flask applications, you generally want a format of:
Application folder
- app.py
- other.py
static/
- css
- js
- img
templates/
- index.html
- other.html

Related

How to tell passenger_wsgi.py to look for Django project inside another folder?

I'm trying to host a django app on cpanel but i cant find a way to tell passenger_wsgi.py to look for the django project(main file) inside another folder
My site structure is:
/home/project/
tmp/
public/
passenger_wsgi.py
abdi-group/
passenger_wsgi.py:
from abdiGroup.wsgi import application
this works fine if i move everything inside abdi-group/ to /home/project/
I tried this:
passenger_wsgi.py:
from abdi-group.abdiGroup.wsgi import application
but it can't find abdiGroup(django project name) inside abdi-group/
am i missing something?
abdiGroup is supposed to be a folder in your project directory that contains the wsgi file not abdi-group. If you're making it a subfolder add an empty __init__.py to any new folder so Django knows it is a package and can import from it.

Chainging directory of flask project - everything fails

Hi I had followed project organisation in directory ./my_project/
-venv/
-static/
-templates/
--index.html
--login.html
-requirements.txt
-main.py
-views.py
then i wanted to add test folder and reorganise all to following structure:
-venv/
-my_project/
--static/
--templates/
---index.html
---login.html
--requirements.txt
--main.py
--views.py
And when I am running main file, server runs, but if I open on a browser right address there is error 404. In log of a server I see that server recieves request, but something is wrong. I have no idea what is this, because I didn't change anything in code.
You need to make the my_project a Python package by adding an empty __init__.py file inside it.
See this answer here https://stackoverflow.com/a/448279/9379795

Initial setup of Flask Mega Tutorial on pythonanywhere

After successfully completing the pythonanywhere flask tutorial (pa.com), Miguel Grinberg's the "Flask Mega Tutorial" (fmt) beckoned. Sadly, I've not even made it to "Hello, World". Here's what I have done:
In pa.com attempting to follow fmt verbatim is a no go:
python3 -m venv flask
leads to an error of
ensurepip is not available
and we do not have sudo access.
Undeterred, I reasoned that all Miguel is asking us to do is distribute the functionality we see in one file in the pa.com tutorial (flask_app.py) into a few files that will make the building of a full app easier. Since pa.com is already setting up my base web app with flask and python 3.4, not being able to set up the virtual env. did not seem to be a block, at least not at first.
Per the fmt, in the base dir of the pa.com (pwd -> home/{username}/microblog) -- which is where the flask_app.py file that successfully generates the pa.com tutorial page lives -- I set up app and tmp directories, and create app/__init__.py, app/views.py and the run.py files as directed by fmt
Hitting the app page (run.py the only file in the main directory) generates an Unhandled Exception on the page.
Changing the name to flask_app.py (which seems to be what pa.com expects on flask installations) generates the same error.
Modifying the content of the flask_app.py code to:
from flask import Flask
app = Flask(__name__)
#app.route('/')
def hello_world():
return "working from flask_app.py"
Generates a successful output from the app, whereas having the same code in a file named run.py generates the same Unhandled Exception error.
the lines:
from app import app in both run.py and views.py and
from app import views in __init__.py
all make me wonder... where is this "app" module coming from? But aside from being puzzled by that question, no other ideas on how to proceed from here. Any suggestions? Would really like to get set up on pa.com and work through this tutorial/book.
Feel like I am missing something basic, but not sure what.
First rule is: don't use app.run() on PythonAnywhere - that's what run.py is trying to do. That's fine for your own PC, but on PA it will cause an error. It's fine to have the file there, but don't try and import from that file in your wsgi config.
Instead, you just need to import the flask app variable, which Miguel gets you to put in app/__init__.py (that's slightly confusing, a variable called app, and a folder called app, but we can deal with it!)
To do that, you'll want to add the folder that contains the app folder to your sys.path. You also need to "rename" the app variable to application as you import it:
# assuming we have /home/myusername/microblog/app/__init__.py:
path = '/home/myusername/microblog'
if path not in sys.path:
sys.path.append(path)
# now we can import the app variable from the app folder's __init__
# and rename it to application
from app import app as application
More info: a brief guide to flask on pythonanywhere and a guide to debugging imports and sys.path problems in your pythonanywhere wsgi file
from microblog import app as application
This is fixed my solution.
Best Regards

Creating a lib directory in Google App Engine and adding it to sys.path

I'm writing a Google App Engine project in Python with Flask. Here's my directory structure for a Hello, World! app (contents of third party libraries ommitted for brevity's sake):
project_root/
flask/
jinja2/
markupsafe/
myapp/
__init__.py
simplejson/
werkzeug/
app.yaml
itsdangerous.py
main.py
Here's main.py:
from google.appengine.ext.webapp.util import run_wsigi_app
from myapp import app
run_wsgi_app(app)
And myapp/__init__.py:
from flask import Flask
app = Flask("myapp")
#app.route("/")
def hello():
return "Hello, World!"
Since Flask has so many dependencies and sub dependencies, I thought it would be nice to tidy up the directory structure by putting all the third party code in a subdirectory (say project_root/lib). Of course, then sys.path doesn't know where to find the libraries.
I've tried the solutions in How do you modify sys.path in Google App Engine (Python)?, but that doesn't seem to work. I've also tried changing from flask import Flask to from lib/flask import Flask, to no avail. Is there a good way to do this?
Add a python file called appengine_config.py with the following content.
import sys
import os.path
sys.path.insert(0, os.path.join(os.path.dirname(__file__), 'lib'))
As Tim Hoffman has mentioned the appengine_config.py is called once when a new instance is started.
Now you can do what you intended. adding all the third-party libraries to the lib folder.
Look at defining your path manipulation in appengine_config.py This means any path manipulation only has to be performed once.
Then move your third party files in to a lib as per your suggestion.
Use a relative path 'lib' either by specifying sys.path
sys.path.insert(0,'./lib')
or use
import site
site.addsitedir("./lib")
Do not use absolute paths, as they won't be the same when you deploy your code.
Putting your lib first sometimes can be useful, especially if you don't want the google supplied version of webob.
try to add the full path to your lib, like here sys.path[0:0] = ['/home/user/project_root/lib']
I've actually run into this problem a number of time when writing my own Google App EngineĀ® products before. My solution was to cat the files together to form one large python file. The command to do this is:
cat *.py
Of course, you may need to fix up the import statements inside the individual files. This can be done with a simple sed command to search for import statements in the files that match file names used in the cat command. But this is a relatively simple command compared to actually creating the huge python file (HPF, for short).
Another advantage of combining them all is increased speed: Because python doesn't need to go loading a whole bunch of useless tiny files all the time, it will run much faster. This means that the users of your Google App EngineĀ® product will not need to wait for crazy long file load latency.
Of course, updating the libraries can be tricky. I recommend keeping the directory tree you used to cat the files together in order to easily update things in the future. All you need to do is copy the new files in and re-run the cat and sed commands. Think of it like pre-compiling your python library.
All the best!
-Milo

Google App Engine won't recognize facebook package unless I rename it

I'm trying to intergrate Facebook Connect into an GAE app. I've got a basic folder structure like so:
/gae-root
/myapp
/templates
/etc
app.yaml
settings.py
and I tried to add the PyFacebook library like so:
/gae-root
/myapp
/templates
/etc
/facebook
/djangofb
app.yaml
settings.py
I thought this would work, but now when I try to import facebook it throws a module not found error. What's even weirder is that if I rename the directory from facebook to foo, the import now works but I'll hit errors later when I try to get the current logged in user.
Literally all I did was move the directory into my folder structure and try an import. What am I missing? Sorry if this is an easy question.
It was a problem with an extra .pth file in my site-packages directory.

Categories