Notes tagged with "Python"
Command Line Libraries
Python Command Line Libraries
I really don’t like any of the cli libraries out there. The one I like best is docopt
and mainly because it is simple to understand and use. The problem with docopt
is that it only does command line parsing. It does not handle argument
type conversion or validation, nor does it do dispatching. It does leave your
cli interface decoupled from your application, whereas some of these other convience libraries,
such as click, couple the building of cli commands and arguments to functions and tend
to make heavy use of decorators - a style I don’t find appealing. They also
tend toward being application frameworks - something I don’t mind, but I’d rather have
more choice over how things are implemented.
Notes on Flask
Notes from Armin Ronacher’s Flask for Fun and Profit
create_app
from flask import Flask
def create_app(config=None):
app = Flask(__name__)
app.config.update(config or {})
register_blueprints(app)
register_other_things(app)
return app
register_blueprints
from werkzeug.utils import find_modules, import_string
def register_blueprints(app):
for name in find_modules('myapp.blueprints'):
mod = import_string(name)
if hasattr(mod, 'blueprint'):
app.register_blueprint(mod.blueprint)
Optional Contained App
The idea here is you can separate top level config and functionality that is clearly separated from internal flask related config and functionality. I.e. in the flask app this object is exposed as an attribute on the app, and visa versa.
Poetry Uploads
Uploading Packages to PyPi With Poetry
Setting a pypi repository token:
-
create a token on pypi. Create a specific token for each package.
-
copy the token and put it somewhere safe.
-
Save your token with poetry. Poetry will try to use keyring to store the token:
poetry config pypi-token.dover-test
poetry config pyi-token.pypi my-token
this is what poetry does
-
fetches the repo url from your local config file (On Linux Mint this is located here: ~/.config/pypoetry/config.toml. )
Working With Poetry
Installing A Working Environment With An Existing Project
This always messes me up.
-
Run
poetry install. This will create a new virtual env and install all your dependencies in it.... poetry install -
Now to use it properly, you need to be in the shell:
... poetry shell -
From here any application commands you have registered in your poetry
pyproject.tomlfile should work:... app-cmd --helpIn theory, you should be able to run this outside the shell:
Manage Static Sites
-
On the server run:
... python3.6 nginx_site.py create my.sitename.com -
Change the access for the root of the site folder so we can easily push changes:
... sudo chown user:user /var/www/com_sitename_my -
Now we can push the static files generated by Pelican to the server:
# the manual way ... scp -r output/* mark.staticsites:/var/www/thebitsilo_com_notes # or via pelican ... inv publish
Web Application Setup - Django Setup
DJANGO SITE SETUP
-
Create virutualenv for you application:
... cd /var/www/appname ... python3 -m virtualenv --python=python3 venv -
Copy the basic application files to your new app directory:
From your development machine:
-
scp manage.py wsgi.py adminusr@server:/var/www/appname
-
scp django.settings.ini adminusr@server:/var/www/appname/config
-
scp requirements.txt adminusr@server:/var/www/appname/packages
-
scp appsite-0.1.0.tar.gz adminusr@server:/var/www/appname/packages
-
scp app-plugin-0.1.0.tar.gz adminusr@server:/var/www/appname/packages
Your directory structure should now look like:
appname ├── config │ ├── django.settings.ini ├── data ├── logs ├── manage.py ├── packages │ ├── appsite-0.1.0.tar.gz │ ├── app-plugin-0.1.0.tar.gz │ ├── requirements.txt ├── static └── wsgi.pyWe’ll describe these files in more detail later.
-
Web Application Setup - Flask Setup
FLASK SITE SETUP
-
Create virutualenv for you application:
... cd /var/www/appname ... python3 -m virtualenv --python=python3 venv -
Copy the basic application files to your new app directory:
From your development machine:
-
scp manage.py wsgi.py adminusr@server:/var/www/appname
-
scp django.settings.ini adminusr@server:/var/www/appname/config
-
scp requirements.txt adminusr@server:/var/www/appname/packages
-
scp appsite-0.1.0.tar.gz adminusr@server:/var/www/appname/packages
-
scp app-plugin-0.1.0.tar.gz adminusr@server:/var/www/appname/packages
Your directory structure should now look like:
appname ├── config │ ├── django.settings.ini ├── data ├── logs ├── manage.py ├── packages │ ├── appsite-0.1.0.tar.gz │ ├── app-plugin-0.1.0.tar.gz │ ├── requirements.txt ├── static └── wsgi.pyWe’ll describe these files in more detail later.
-
Web Application Setup - Install Software
Install Software
-
Install Python 3
Good news, python 3 is already install on Ubuntu 17.10.
-
Install other top level software packages:
You may need to run ubuntu upgrade before insalling:
... sudo apt-get update ... sudo apt-get upgradeInstall the following packages:
... sudo apt-get install python3.7 ... sudo apt-get install python3.7-venv ... sudo apt-get install python3-pip ... sudo apt-get install uwsgi-core ... sudo apt-get install uwsgi-plugin-python3 ... sudo apt-get install nginxAll other python packages will be installed in the applications virtual environment.
Web Application Setup - uWSGI Set Up
Set Up uWSGI To Server Your Application
-
Create your uWSGI configuration file:
... touch /var/www/appname/config/uwsgi-appname.ini -
Contents of your
uwsgi-appname.inishould look like this:[uwsgi] plugins = python3,logfile chdir = /var/www/appname home = /var/www/appname/venv wsgi-file = /var/www/appname/wsgi.py master = True cheap = True idle = 600 die-on-idle = True manage-script-name = True -
Link your config file so uwsgi can find it:
... cd /etc/uwsgi/apps-enabled ... ln -s /var/www/appname/config/uwsgi-appname.ini appname.ini
SYSTEMD UWSGI SET UP
-
Create systemd socket and server files for your uwsgi app: