CyberKeeda In Social Media

How to make your Python Django App Reusable


Django is cool and it's features like any other python libraries and frameworks are way more cooler then it.

One of the best feature is to make your django app easily distributable for re usability.

Within this blog post, we will cover.
  • How can we build a django app a python package as xyz.tar.gz
  • How can be uploaded our created django app python package to Python Package Index, simple know as PyPI.
  • How we can Install our created package locally via tar.gz file.
  • How can we install using standard pip command as "pip install your_py_package"
Assumptions.
  • We assume you already have a running Django Project and beneath it there must be an application, which you want to package and make it disributable.
  • Here in this blog post we already have a Django project named as docdocGo and within it we have created a django application named as FilesNow, we will cover all things taking it an example.
  • FilesNow is an django application, that can be used to download contents from a AWS S3 bucket within it's temporary directory and serve it as a presentable media to view or download for a user, it deletes the files after a fix set interval. 
  • FilesNow on GitHub linkhttps://github.com/Jackuna/django-filesnow
Here is how our django project and it's  directory look like.

Dependencies

  • Python 3+
  • Django 2.2+
  • PIP
  • Twine ( pip install twin )

Lets proceed and how to Package our filesnow django app.
  1. Create a new empty directory outside our django project and name it something relevant to your app, here we will use it name as django-filesnow.
  2. Copy the entire application directory and paste under newly created directory.
  3. Toggle to directory django-filesnow and lets move ahead and create few required files within it.
  • Create a file django-filesnow/README.rst with the below content and do replace with your own.
Content of django-filesnow/README.rst
=========
FilesNow
=========

FilesNow is a Django app to download documnets, images 
from AWS S3 and serve is a temporary static content to customers.

FilesNow is a way to serve AWS S3 documents/media files
without giving access to your s3 buckets.

FilesNow itself cleans it's downloaded presentable
files, as such maintainig a healthy file system

Dependecies
-----------
AWS Boto3 Framework : pip install boto3
Configure AWS Credentilas using command : aws configure

Quick start
-----------

1. Add "filesnow" to your INSTALLED_APPS setting like this::

    INSTALLED_APPS = [
        ...
        'filesnow',
    ]

2. Include the polls URLconf in your project urls.py like this::

    path('filesnow/', include('filesnow.urls'))

 
3. Start the development server ``python manage.py runserver 0.0.0.0:9090``

4. Visit http://127.0.0.1:9090/filesnow and explore it.
  • Create a license file django-filesnow/LICENSE , choose license as per requirement of yours ( GNU, BSD, MIT ) etc, Choose a license website ( https://choosealicense.com/ ) can help you to guide about your required license and it's content. I have used MIT license and you can find it's content within my Github repo.
  • Create two setup files within same directory and name it as django-filesnow/setup.cfg   and django-filesnow/setup.py with the below content and do replace with your own.
Content of django-filesnow/setup.cfg
[metadata]
name = django-filesnow
version = 0.1
description = A Django app to download cloud contents.
long_description = file: README.rst
url = https://github.com/Jackuna/django-filesnow
author = Jackuna
author_email = admin@cyberkeeda.com
license = MIT License
classifiers =
    Environment :: Web Environment
    Framework :: Django
 Framework :: Django :: 2.2
    Intended Audience :: Developers
    License :: OSI Approved :: MIT License
    Operating System :: OS Independent
    Programming Language :: Python
    Programming Language :: Python :: 3
    Programming Language :: Python :: 3 :: Only
    Programming Language :: Python :: 3.6
    Programming Language :: Python :: 3.7
    Programming Language :: Python :: 3.8
    Topic :: Internet :: WWW/HTTP
    Topic :: Internet :: WWW/HTTP :: Dynamic Content

[options]
include_package_data = true
packages = find:
Content of django-filesnow/setup.py
from setuptools import setup

setup()
  • Only Python modules and packages are included in the package by default. To include additional files, we’ll need to create a MANIFEST.in file.  To include the templates, the README.rst and our LICENSE file, create a file django-polls/MANIFEST.in with the following contents:
Content of django-filesnow/MANIFEST.in
include LICENSE
include README.rst
recursive-include filesnow/static *
recursive-include filesnow/templates *
So now we are all done with creation of required files in order to package your app, lets toggle again into the parent directory ( django-filesnow )and open a terminal/command prompt and run the below command to build our package.
# python setup.py sdist
Once command ends with successfully, and additional directory with name "dist" will be created, it contains our bundled and packaged django app, in our case  django-filesnow-0.1.tar.gz has been created.

Thus we have packaged our django app by name django-filesnow-0.1.tar.gz, we can distribute it as per your requirement, like uploading to repositories like github, email, uploading to any forum or website.

Let's know how to install our packaged filesnow django app django-filesnow-0.1.tar.gz, use the below command to install it.
# python -m pip install --user django-polls-0.1.tar.gz
Within Above command will install it, for windows we can located the installed application by name "filesnow" under directory.

"C:\Users\Jackuna\AppData\Roaming\Python\Python37\site-packages\filesnow"

Please note:
Above highlighted in red may varry, depending upon your system and package.
Package will be installed by name filesnow only not django-filesnow.
Within next post, we will know the remaining topics !

No comments:

Post a Comment

Related Posts Plugin for WordPress, Blogger...
Designed By Jackuna