Tag: Django

  • Why, When, and How to use Celery with Python

    “Celery is an asynchronous task queue based on distributed message passing.”

    Let’s break that down:

    (more…)
  • Setting up a Python Virtual Environment for Web Development

    Virtual Environments are helpful in keeping all the dependencies (packages), required for a Python project, in one place. So you can avoid issues with conflicting dependencies when working on different projects simultaneously. For instance, one major use case is when you want to run different Python versions like 2.7, 3.5, and 3.6 on the same system.

    You can use the following steps when setting up any Python project (including web development project using frameworks like Django, Flask etc.)

    1. Make a top-level Directory

    Create a directory named “virtualenvs” (a parent directory to keep all your virtual environments). All your Python projects will live inside this directory.
    mkdir virtualenvs

    2. Create the Python Virtual Environment

    cd virtualenvs
    python3 -m venv env

    You may use any name for the directory, other than “env”. If you’re curious about what goes inside the auto-generated “env” directory, you can check here:
    cd env
    cat pyvenv.cfg
    python —version
    pip —version

    3. Activate the virtual env

    On Mac: source bin/activate
    On Linux: ./bin/activate
    On Windows: .\Scripts\activate

    Important: You need to activate the virtual environment every time you want to build or run the code. You may edit the code files without activating the venv.
    Use deactivate to leave the virtual environment, or simply close the session/Terminal.

    4. Create the project directory

    mkdir your_proj_name

    Optionally, Initialise git inside your_proj_name directory (you don’t need to commit the other files of env directory into git.):

    git init
    OR if you have the project code already, git clone the code inside env:
    git clone [url]

    5 a. Install packages/dependencies using pip

    If you’ve a requirements.txt file:
    pip install -r requirements.txt

    or install the packages individually, for example for Django:
    pip install django or
    pip install django==2.1 # For a particular version

    5 b. You can also install packages from Source Code

    For example download Django pre-release code from their git repo, and unzip it into a directory named django, then:

    pip install -e django
    OR
    pip install --editable django

    Note that you may run pip from any of env‘s subdirectories: the installed code will always go into the env/lib and env/bin directories.

    6. Finish and Run

    You’re set to start working on the project now.
    If you’re satisfied with the setup, you most probably want to keep a list of dependencies you just installed.

    pip freeze > requirements.txt

    (Keep the requirements file in git if you’ve initialised the git repo.)

    7. Optimizing your daily workflow

    I made this small shell script to automate the task of entering and activating the venv every-time I start:

    #!/bin/bash
    cd virtualenvs/env/
    source bin/activate
    # Configure any other environment variables here -
    # for example, private API keys that you don't want to check in to git.
    cd your_proj_name/
    

    Paste the above code in a file name  start_your_project_name.sh. From now on, to get started, you just have to do this:

    source start_your_project_name.sh
    or ./start_your_project_name.sh
  • Django’s Request-Response Cycle

    Understanding the Request-Response Cycle is a crucial step in setting up for any WebServer.

    An example of the request-response cycle is when you “request” a web page by entering a URL in your browser and a web server sends a web page as ‘response’ to your browser. Of course, there’re usually several HTTP requests that happen when visiting a website; each is a separate Request-Response in itself.

    The request-response architecture of a web application is an important idea to grasp whether you’re working on Django, Symphony, Laravel, Flask, WordPress or any of the thousands of other web frameworks.

    When you call a Django server the flow typically is

    Request: Browser → Web Server → WSGI → WSGI callable function in Django

    Response: Django → WSGI → Web Server → Browser

    Here’s a diagram that explains the request-response flow very well.

    Django's Request-Response Call Cycle

  • Python for beginners

    Getting functional in Python programming took me about a month and I’ve several years of C++ programming experience. I feel this is ok time for experienced programmers to get started, given you have something else going on in life as well. For beginners though, learning a new programming language in a useful manner is not as casual an affair as some YouTubers and Tech bloggers would tend to suggest on the internet (looking at you people, who write the ‘learn X in 1 hour’ guides 🙄).

    Computer systems need a fixed syntax and grammar which needs practice to get comfortable with. Python is easier than many other languages, but still, if you’re a beginner, you need to set aside a good amount of time learning it. People will forget to mention this when recommending books, but there are broadly two categories of programming books out there:

    • 1) for experienced programmers/Web Developers, and
    • 2) for beginners, with the language basics.

    If you’re a beginner to programming, don’t pick the former.

    If you just want to write some standalone scripts, then avoid books targeting Web Developers (Web developers would have to learn other topics, like a web framework, HTML rendering, REST APIs, etc., stuff that you can skip). I learnt it from Dive into Python, which is a bit advanced and not for you if this is the first computer language you are learning. It’s a great book though!

    If you’re a beginner, just focus on the language syntax, keywords and the style of writing code. The book Byte of Python might be it for you. Or try Think Python.

    Don’t try to learn by collecting tips from forums on random Python-related FAQs. It’s best if you start with a complete book. The forums will come in handy later when you have started coding and are stuck at some specific point. I haven’t studied programming through interactive online websites yet; I prefer working with native tools and find practising on the browser a bit cumbersome. It’s always better to practice coding in the environment you would be working on. Online interactive tutorials will give you a sense of accomplishment, with a quick start, but without actually building a good foundation. Having said that, there are a few good resources out there.

    There are also some very good online video courses available now. “sentdex” on youtube has some awesome content.


    P.S.: Python like most scripting languages uses Regular Expressions for a lot of text operations: it would help you greatly if you learn a bit of RegEx as well (You have definitely encountered some RegEx already if you’re an active computer user).

    If you’re preparing for Web Development: then learning about web frameworks (ex. Django), file I/O, REST API, Databases etc. will be the next steps. Pick the advanced topics after you’ve learnt the language basics though.