I'm writing an application in python using CherryPy and Jinja as the template system. May be needed to say that i'm a beginner with these tools.
The problem I'm facing now is I cannot figure where to initialize Jinja's Environment class.
Currently I have
application.py (entry point, sets up Environment and starts server)
root.py (root page class for cherrypy, must be imported from 'application.py', and must import 'application.py' to retrieve instantiated Enviroment)
pages.py (other page classes for cherry.py, must import 'application.py', and must be imported from root to build the tree)
Trying to run that ends up in what seems to be a circular reference and fails (application > root > pages > application).
Should I stick to only one Environment instance or is it okay to have an instance at root.py and another in pages.py?
Which is the correct pattern for this?
You shouldn't really repeat yourself. If I were you I would create a new python module templates.py and put all the jinja environment configuration / creation there. Afterwards you can simply import that environment wherever you need it (e.g. from templates import jinjaenv). In this case you keep things simple and extensible for future use.
Related
I have this python project with three directories, my_dir1, my_dir2, and my_dir3. Within my_dir1 I have defined a plugin that I would like to use for my_dir2 but not my_dir3. The problem is that even though I have configured the conftest.py for my_dir2 to register the plugin, it seems to be applied to my_dir3 as well. Since I do not want the plugin to apply to my_dir3, how do I prevent this from happening?
P.S. I should add that methods in my_dir3 are invoking methods in my_dir2
The primary problem I'm trying to solve is how to detect all the subclasses of a particular class. The reason I'm unable to use __subclasses__ is that the child classes aren't yet accessible in the context from which I'm attempting to access them.
The folder structure I'm working with looks like this:
main.py
projects/
__init__.py
project.py
some_project_child.py
What I'd like to do is get a list of all subclasses of Project (defined in project.py) from main.py.
I'm able to do this by doing:
from projects.project import Project
from projects.some_project_child import SomeProjectChild
Project.__subclasses__
The aspect of this approach I'd like to avoid is having to add an import line every time I add a new project file/class. I realize I can do this by iterating over the files in the directory and import each one's contents but is there a cleaner more pythonic manner of handling this?
It is the nature of a non-compiled language like Python that it is impossible to do anything like this without importing. There is simply no way for Python to know what subclasses any class has without executing the files they are defined in.
i want to setup aiohttp_jinja2 for multiple loader (PackageLoader, FileSystemLoader), for example:
def setup_template_engine(app):
aiohttp_jinja2.setup(app, loader=jinja2.FileSystemLoader(str(Path('.') / 'templates')))
aiohttp_jinja2.setup(app, loader=jinja2.PackageLoader('app', 'templates')) # in this case, only last setup works
and question is: how to setup aiohttp_jinja2 for multiple loader?
I think you might think about creating multiple environments, as suggested here: https://jinja.palletsprojects.com/en/master/api/#basics
There they say:
Most applications will create one Environment object on application initialization and use that to load templates. In some cases however, it’s useful to have multiple environments side by side, if different configurations are in use.
Then you would have one environment with the FileSystemLoader and one with the PackageLoader
I am creating an application consisting of several modules. There is one main.py file which will be the file to run the application. The main.py file will load the configuration file(s) and put them in the 'config'-variable. It will also import the application-module-file (the file which holds the source-code of the application itself, a.k.a. application-class) and start the instance.
I am not very experienced in coding Python, and my biggest question is if I am doing it the right way, by using a main-file to handle all needed stuff (loading configuration-files for example). The problem I am having right now is that I cannot access the 'config'-variable that was defined in the main.py-file from any other module and/or Python-file.
Is it possible to make a global variable for configuration-values exc.? I know in PHP I used to create a singleton object which holds all the specific global arguments. I could also create a global 'ROOT'-variable to hold the full path to the root of the application, which is needed to load/import new files, this is also not possible in Python as far as I know.
I hope someone can help me out of this or send me in the right direction so I can continue working on this project.
The answer seems to be by Matthias:
Use from AppName.modules import settings and then access the data in the module with settings.value. According to PEP-8, the style guide for Python code, wildcard imports should be avoided and would in fact lead to undesirable behaviour in this case.
Thanks you all for the help!
I found a custom field model (JSONField) that I would like to integrate into my Django project.
Where do I actually put the JSONField.py file? -- Would it reside in my Django project or would I put it in something like: /django/db/models/fields/
Since I assume it can be done multiple ways, would it then impact how JSONField (or any custom field for that matter) would get imported into my models.py file as well?
It's worth remembering that Django is just Python, and so the same rules apply to Django customisations as they would for any other random Python library you might download. To use a bit of code, it has to be in a module somewhere on your Pythonpath, and then you can just to from foo import x.
I sometimes have a lib directory within my Django project structure, and put into it all the various things I might need to import. In this case I might put the JSONField code into a module called fields, as I might have other customised fields.
Since I know my project is already on the Pythonpath, I can just do from lib.fields import JSONField, then I can just do myfield = JSONField(options) in the model definition.
For the first question, I would rather not put it into django directory, because in case of upgrades you may end up loosing all of your changes. It is a general point: modifying an external piece of code will lead to increased maintenance costs.
Therefore, I would suggest you putting it into some place accessible from your pythonpath - it could be a module in your project, or directly inside the site-packages directory.
As about the second question, just "installing" it will not impact your existing models.
You have to explicitly use it, by either by adding it to all of your models that need it, either by defining a model that uses it, and from whom all of your models will inherit.
The best thing would be to keep Django and customizations apart. You could place the file anywhere on your pythonpath really