Class-based famework for quickly creating robust Python apps.
Project description
Atlas App Framework
This is a simple framework for creating Class based applications that can be run as a command line application or importated and run as a module. It's goal is to remove a lot of boiler plate that is typically done when writting CLI and GUI apps.
Example
You can run our sample app by checking out the repo, installing the packages and then using the following command line below. It's reconmended that you use a virtual environment and not install packages globally.
> python tests/sample_gui.py
Here is a simple CLI app to show how this all works.
from atlas.app.cli import CLIApp
class SampleApp(CLIApp):
description = "This is a sample App"
def add_arguments(self):
self.add_argument("-s", "--sample", help="A sample CLI argument")
self.add_argument("-v", "--verbose", action="store_true")
def run(self, ctx):
print("Hello All")
def main():
"""This the function that instantiates the App Class and runs it. This is to make it easy to create 'project.scripts'
in your pyproject.toml to run command from the command line.
"""
app = SampleApp()
app()
if __name__ == "__main__":
main()
This first step imports the app class to inherit from. In this case it's the CLIApp class.
from atlas.app.cli import CLIApp
The next thing we do is subclass the base app class (CLIApp) to make our own App Class.
class SampleApp(CLIApp):
pass
If we just run this, we will get the default output (which is to print our execution context to the screen). This is good to show that it works but it's not what we really want to do.
Right now our app does not have any command line arguments so we need to add some.
def add_arguments(self):
self.add_argument("-s", "--sample", help="A sample CLI argument")
self.add_argument("-v", "--verbose", action="store_true")
By overriding the add_arguments()
method we add our arguments in there one at a time. It's the same syntax as Python's ArgParse library so there is a lot of documentation on the format available. If you run the app again, you will now notice that the context output shows two more keys, "sample" and "verbose".
The next thing to do is add some business logic to our app by overriding the run()
method. In the example it's this:
def run(self, ctx):
print("Hello All")
That is pretty easy. Inside the run() method we have access to the context in which we are executing our code. The Context() object (ctx) is an advanced dictionary that brings together a bunch of pieces of data from a configuation file, the environment, and command line arguments into a single context with each overriding the values from the prior. So anything set in a configuation file is overwritten by environment variables which are then overridden by command line arguments.
That's the basic use of the framework.
Making the app available on the Command Line
The last two parts of te example show how you can launch your app.
if __name__ == "__main__":
main()
The last part here is a traditional way where Python will run the function main()
if it's in the first file the Python interpreter loads. This works for demonstration purposes but it's not a very intuitive way to do things. It requires typing on the command line a long string that does not look like our app (python test/sample.py -s "bob"
for example). We want something cleaner than that like sample_app -s "bob"
.
To do this we will leverage a convention used when packaging our app. In setup.py they are called "entry_points" and in pyproject.toml they are called 'project.scripts'.
The first step is with this:
def main():
"""This the function that instantiates the App Class and runs it. This is to make it easy to create 'project.scripts'
in your pyproject.toml to run command from the command line.
"""
app = SampleApp()
app()
We define a function called main()
that instatiates our App Class and then calls it. With the main() function defines, in our pyproject.toml we add this to our code.
[project.scripts]
sample_app = "test.sample:main"
...and for setup.py (the older, deprecated way) it looks like this:
setup(
# ...,
entry_points={
'console_scripts': [
'sample_app = test.sample:main',
]
}
)
In both cases you will notice it's the name of the CLI app sample_app
followed by =
and then the import path to the module (test.sample
), followed by a :
and the method to call (main
). When you install the package it will also link the executeable to work in your command line shell on Linux, MacOS and Windows.
Project details
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
File details
Details for the file atlas-framework-0.1.3.tar.gz
.
File metadata
- Download URL: atlas-framework-0.1.3.tar.gz
- Upload date:
- Size: 9.7 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/4.0.2 CPython/3.9.16
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 6293cf399ca5bed6aa44cc368871d7025cae64ec76e23e1ad3b69a679b5b0310 |
|
MD5 | e32b5632458e2add3f8c89d26cd71897 |
|
BLAKE2b-256 | 7bf398b1aa5dc3b0d6a739aebec56cbbdc4041f75d5ab630e36fa906095e7322 |
File details
Details for the file atlas_framework-0.1.3-py2.py3-none-any.whl
.
File metadata
- Download URL: atlas_framework-0.1.3-py2.py3-none-any.whl
- Upload date:
- Size: 8.3 kB
- Tags: Python 2, Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/4.0.2 CPython/3.9.16
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 5382b8c3ee274b7c5695aa5d859de5ad138715c0157503ec5817a355f103e162 |
|
MD5 | 8981d85b1e71e3e4cd4243f1fe70b9e0 |
|
BLAKE2b-256 | 7a40994b76f8274ca5989e5757ee4978cc320baae08167a006f71aacf2357a4e |