Working with live views in Swift Playground

Prototype faster in Swift Playground without Projects

When working on prototypes it is easy to create a new Playground to experiment on views. This makes the development really fast and easy. This method is much faster than creating a new XCode Project for prototyping.

Below is the code I use in Swift Playground

import UIKit
import PlaygroundSupport

let f = CGRect(x: 0.0, y: 0.0, width: 50.0, height: 50.0)
let v = UIView(frame: f)
v.backgroundColor = UIColor.yellow

PlaygroundPage.current.liveView = v


Then, to see the live view, select ViewAssistant EditorShow Assistant Editor

Show Assistant Editor in XCode


And then select Timeline option to see the live view.

Show Playground Timeline in Assistant Editor (XCode)


Later, we will add UIView with red background color as sub-view to test live updates.

// will be adding red view as sub view
let redView = UIView(frame:
redView.translatesAutoresizingMaskIntoConstraints = false
redView.backgroundColor =


redView.centerXAnchor.constraint(equalTo: v.centerXAnchor, constant: 0.0).isActive = true
redView.centerYAnchor.constraint(equalTo: v.centerYAnchor, constant: 0.0).isActive = true
redView.widthAnchor.constraint(equalToConstant: 15.0).isActive = true
redView.heightAnchor.constraint(equalToConstant: 15.0).isActive = true


And finally, see the updates.

See live updates in Playground (XCode)

Adding https support in Flask

Getting a chance to work on Python, my favorite framework is Flask ( It is a micro-framework that lets you create and deploy APIs, websites, and dashboards.

When deploying Flask projects onto a production server, we need to only support HTTPS and redirect HTTP requests to HTTPS. This creates a problem with Flask url_for function, which only outputs HTTP URLs.

Below is one way to achieve this. Following is taken from my production deployment.

import os
from flask import Flask

app = Flask(__name__)

def before_request():
    if os.environ["PROJECT_ENV"] != "dev":
        from flask import _request_ctx_stack
        if _request_ctx_stack is not None:
            reqctx =
            reqctx.url_adapter.url_scheme = "https"

if __name__ == "__main__":, host="")


Update: 8 June 2017

I have found another way of adding https support to flask.

from flask import Flask

class ReverseProxied(object):
        def __init__(self, app):
       = app

        def __call__(self, environ, start_response):
                environ['wsgi.url_scheme'] = 'https'
                return, start_response)

app = Flask(__name__)
app.wsgi_app = ReverseProxied(app.wsgi_app)

if __name__ == "__main__":, host="")

This was mentioned in stack overflow post:

Let me know if you have a better approach to this problem.

Noto Fonts: Beautiful and free fonts for all languages

Google has provided and open-sourced Noto fonts family covering all the languages. The complete font package is over 100MB, which can be downloaded as one or individual fonts.

I have recently used Noto Kufi Arabic font in Al Arabiya News (Arabic) iOS Application. Check out how neat and readable the text renders.

To find out more and check out other fonts, head over to .

Generate Random Colors in Swift

Randomize UIColor

We have all come across scenarios where we need to use random colors. For example, showing UIViewControllers with random background color in a UIPageViewController, or just show UITableViewCells with random background colors in a UITableView.

I have created an extension on UIColor to help me in the applications.

Here is the code in Swift 3:

extension UIColor {
    static var random: UIColor {
        // Seed (only once)
        return UIColor(red: CGFloat(drand48()), green: CGFloat(drand48()), blue: CGFloat(drand48()), alpha: 1.0)



view.backgroundColor = UIColor.random


Here is the github repo for the sample project: