Unable to solve Flask blueprint 404 error - python-3.x

I am currently working on a flask_app. This is my project structure
├── pypi_org
│   ├── __pycache__
│   │   └── app.cpython-37.pyc
│   ├── app.py
│   ├── services
│   │   └── package_services.py
│   ├── static
│   │   ├── css
│   │   │   └── site.css
│   │   ├── html
│   │   └── js
│   ├── templates
│   │   ├── home
│   │   │   ├── about.html
│   │   │   └── index.html
│   │   ├── packages
│   │   │   └── details.html
│   │   └── shared
│   │   └── _layout.html
│   ├── tests
│   ├── viewmodels
│   └── views
│   ├── home_views.py
│   └── package_views.py
├── requirements-dev.txt
└── requirements.text
I have defined the blueprint in home_views.py
from flask import render_template
from flask import Blueprint
from pypi_org.services import package_services
blueprint = Blueprint('home', __name__, template_folder='templates')
#blueprint.route('/')
def index():
test_packages = package_services.get_latest_packages()
return render_template('home/index.html', packages=test_packages)
#blueprint.route('/about')
def about():
return render_template('home/about.html')
Code for app.py is given below
from flask import Flask
from pypi_org.views import home_views, package_views
app = Flask(__name__)
def main():
register_blueprints()
app.run(debug=True)
def register_blueprints():
app.register_blueprint(home_views.blueprint)
app.register_blueprint(package_views.blueprint)
if __name__ == '__main__':
main()
When I run the app.py I get the 404 error.
I'm not sure what I am doing wrong. Everything looks right.
Can someone take a look?

I ran into the exact same issue within the same course.
If you right-click the app.py file and select run, Pycharm will create a run configuration, in which it will execute "app.run". It won't care about what you defined in your main(), and so the register_blueprints() will not be run!
The same run configuration will be created, if you create one yourself selecting Flask server. This would be again a bad idea.
To overcome this issue, create a run configuration where you select python. The same way as you would run any python script. This way Pycharm will execute "python app.py" (not "app.run") and it will run all lines in that file.

Related

importing folder that shadows same name as third party dependency. What to do?

say I have a folder structure that looks like this:
.
├── CODEOWNERS
├── Makefile
├── README.rst
├── __pycache__
│   └── conftest.cpython-37-pytest-6.2.2.pyc
├── airflow
│   ├── __pycache__
│   │   └── __init__.cpython-37.pyc
│   ├── dev
│   │   └── dags
│   └── <some_file_I_need>
how do I import the file I need from the airflow local package (not the third party dependency named airflow). I have a dependency called airflow unfortunately and that gets imported when I do: import airflow.dev... and that errors out.

Create localized django app and use the localization from other app

I have the following problem:
I created a Django app (app1) and then installed it in other one (app2). Now I'm trying to make the internationalization of the site, but I want to be able to use the installed app translations and I cannot even compile them.
Some useful information:
APP 1
.
├── MANIFEST.in
├── app1
│   ├── admin.py
│   ├── apps.py
│   ├── forms.py
│   ├── __init__.py
│   ├── locale/
│   │   ├── en-us
│   │   │   └── LC_MESSAGES
│   │   │   └── django.po
│   │   ├── es
│   │   │   └── LC_MESSAGES
│   │   │   └── django.po
│   │   └── pr
│   │   └── LC_MESSAGES
│   │   └── django.po
│   ├── migrations/
│   ├── models.py
│   ├── settings.py
│   ├── static/
│   ├── templates
│   ├── tests.py
│   ├── urls.py
│   ├── utils.py
│   └── views.py
└── setup.py
APP 2 (the one that has APP 1 installed)
├── app2/
│   ├── locale/
│   │   ├── en-us/
│   │   │   └── LC_MESSAGES
│   │   │   ├── django.mo
│   │   │   └── django.po
│   │   ├── es/
│   │   │   └── LC_MESSAGES
│   │   │   ├── django.mo
│   │   │   └── django.po
│   │   └── pr/
│   │   └── LC_MESSAGES
│   │   ├── django.mo
│   │   └── django.po
INSTALLED_APPS = [
'django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
'app1.apps.App1SiteConfig',
'app2.apps.App2SiteConfig',
]
LANGUAGE_CODE = 'es'
LANGUAGES = (
('en-us', _('English')),
('es', _('Spanish')),
('pt', _('Portuguese'))
)
LOCALE_PATHS = (
os.path.join(BASE_DIR, "app2", "locale"),
)
Basically, the desired TODOs are:
compile the .mo's from App1
use the App1 transalations (it has its own templates and models so ideally they would be used there)
What I don't want:
compile the App1 .mo's from django-admin of App2 and then translate it there.
Thanks in advance!
Well.. finally I managed to solve this by my way..
With the virtualenv activated I moved to App1 dir and executed django-admin compilemessages, the .mo's has been created in the right path (app1/locale/<lang>/LC_MESSAGES/django.mo) and they're able to be used from the site.

Importing python modules and packages

I have following project structure,
|
├── database
│   ├── db_adapter.py
│   ├── db_layer
│   │   ├── __init__.py
│   │   └── mysql_adapter.py
│   ├── __init__.py
│   └── scripts
│   └── schema.sql
|
└── workflow
├── dags
│   ├── dss_controller.py
│   ├── __init__.py
|
├── __init__.py
├── plugins
I want to import db_adapter.py module inside dss_controller module, when I tried do it following error shown,
ModuleNotFoundError: No module named 'database'
How can I do the correct importing of the modules?

Test project with pytest in different folder

I'm trying to add pytest to a flask app. This is the project structure.
root
├── myapp
│   ├── app.py
│   ├── config.py
│   ├── module
│   │   ├── models.py
│   ├── pages
│   │   ├── forms.py
│   │   ├── templates
│   │   │   ├── base.html
│   │   │   ├── home.html
│   │   │   └── login.html
│   │   └── views.py
├── Pipfile
├── Pipfile.lock
├── __pycache__
└── tests
├── conftest.py
└── test_pages.py
The flask app is under myapp folder. The command flask run runs ok under this folder. So the app is working fine on its own.
The test folder is at the same level as myapp. If I run pytest or py.test I get this error.
conftest.py:3: in <module>
from app import app
E ModuleNotFoundError: No module named 'app'
conftest.py
from pytest import fixture
from app import app
#fixture
def client():
return app.app
I want a way to pytest to recognize myapp as the root of the project. Even if they are in different folders.

Bootstrap in Bower Components - 404 (Not Found)

I've Googled around and searched Stack Overflow but I can't find an answer that has helped me unfortunately.
I have a small Node project with the following layout:
├── app
│   ├── config
│   │   ├── env
│   │   │   ├── development.js
│   │   │   └── production.js
│   │   ├── express.js
│   │   └── index.js
│   ├── controllers
│   │   ├── home.js
│   │   ├── index.js
│   │   ├── metric.js
│   │   ├── metricList.js
│   │   └── metrics
│   │   ├── file2.js
│   │   ├── file3
│   │   ├── file4.js
│   │   ├── file5.js
│   │   ├── file6
│   │   ├── index.js
│   │   └── points.js
│   ├── data
│   │   └── premierLeague
│   │   └── 2015-16.json
│   ├── public
│   │   └── main.css
│   │   └── bower_components
│   ├── routes.js
│   ├── server.js
│   └── views
│   ├── home.handlebars
│   ├── layouts
│   │   └── main.handlebars
│   ├── metric.handlebars
│   └── metricList.handlebars
├── bower.json
├── jsonScript.js
├── npm-debug.log
├── package.json
└── server.js
Bootstrap is located in the bower_components folder under public.
In my app/views/layouts/main.handlebars file I require the bootstrap CSS file using the following script tag:
<link rel="stylesheet" href="bower_components/bootstrap/dist/css/bootstrap.min.css">
In my app/config/express.js file (which does a lot of the work of getting the app going), I have this line:
app.use(express.static('./public'));
Unfortunately I always see this error in the Chrome console when I host up my app:
http://localhost:8080/bower_components/bootstrap/dist/css/bootstrap.min.css Failed to load resource: the server responded with a status of 404 (Not Found)
I'd be really grateful if someone could advise me how to either host my bower_components properly using Express or link to it successfully from my main layout. This is driving me mad, I've tried countless combinations and it never seems to work! Many thanks
Try to get the right path:
app.use(express.static(__dirname + '../public'));
The __dirname get the local directory.
And:
<link rel="stylesheet" href="/bower_components/bootstrap/dist/css/bootstrap.min.css">

Resources