There are various apps presently hosted on apps.loklak.org, which shows several interesting and awesome stuff that can be done using the loklak API. Now the question is how to create a loklak app? Well previously there were two ways to create a loklak app, you could either create an app from scratch creating all the necessary files including index.html (app entry point), app.json (required for maintaining app metadata and store listing), getStarted.md, appUse.md and other necessary files, or you could use the boilerplate app which provides you with a ready to go app template which you have to edit to create your app.
Recently there has been a new addition to the repository, a command line tool called loklakinit, built with python which will automate the process of initiating a loklak app on the go. The tool creates the entire loklak app directory structure including app.json with all the necessary details (as provided by the user) and defaults, index.html, a css and js subdirectory with a style.css and script.js in the respective folders, getStarted.md file, appUse.md file and others.md file with proper references in app.json.
All that the developer needs to do is, execute the following from the root of the apps.loklak.org repository.
bin/loklakinit.sh
This will start the process and initiate the app.
Creating loklakinit tool
Now let us delve into the code. So what actually the script does? How it works? Well the tool acts very much like the popular npm init command. At first the script creates a dictionary which stores the defaults for app.json. Next the script takes a number of inputs from the user and predicts some default values, if the user refuses to provide any input for a particular parameter then the default value for that parameter is used.
app_json = collections.OrderedDict() app_json["@context"] = "http://schema.org" app_json["@type"] = "SoftwareApplication" app_json["permissions"] = "/api/search.json" app_json["name"] = "myloklakapp" app_json["headline"] = "My first loklak app" app_json["alternativeHeadline"] = app_json["headline"] app_json["applicationCategory"] = "Misc" app_json["applicationSubCategory"] = "" app_json["operatingSystem"] = "http://loklak.org" app_json["promoImage"] = "promo.png" app_json["appImages"] = "" app_json["oneLineDescription"] = "" app_json["getStarted"] = "getStarted.md" app_json["appUse"] = "appUse.md" app_json["others"] = "others.md" author = collections.OrderedDict() author["@type"] = "Person" author["name"] = "" author["email"] = "" author["url"] = "" author["sameAs"] = "" app_json["author"] = author
The first part of the script inserts some default values into the app_json dictionary. Now what is an OrderedDict? Well, it is nothing but a python dictionary in which the order in which the keys are inserted is maintained. A ordinary python dictionary does not maintain the order of the keys.
while True : app_name = raw_input("name (" + app_json["name"] + ") : ") if app_name : app_json["name"] = app_name app_context = raw_input("@context (" + app_json["@context"] + ") : ") if app_context : app_json["@context"] = app_context app_type = raw_input("@type (" + app_json["@type"] + ") : ") if app_type : app_json["@type"] = app_type app_permissions = raw_input("permissions (" + app_json["permissions"] + ") : ") if app_permissions : app_json["permissions"] = app_permissions.split(",") app_headline = raw_input("headline (" + app_json["headline"] + ") : ") if app_headline : app_json["headline"] = app_headline app_json["alternativeHeadline"] = app_headline app_alternative_headline = raw_input("alternative headline (" + app_json["alternativeHeadline"] + ") : ") if app_alternative_headline : app_json["alternativeHeadline"] = app_alternative_headline app_category = raw_input("application category (" + app_json["applicationCategory"] + ") : ") if app_category : app_json["applicationCategory"] = app_category app_sub_category = raw_input("sub category : ") if app_sub_category : app_json["applicationSubCategory"] = app_sub_category app_os = raw_input("application operating system (" + app_json["operatingSystem"] + ") : ") if app_os : app_json["operatingSystem"] = app_os app_promo_image = raw_input("promo image (" + app_json["promoImage"] + ") : ") if app_promo_image : app_json["promoImage"] = app_promo_image app_images = raw_input("app preview images (values can be separted by comma) : ") if app_images : app_json["appImages"] = app_images.split(",") app_description = raw_input("one line description : ") if app_description : app_json["oneLineDescription"] = app_description author["name"] = raw_input("author name : ") author_type = raw_input("author type (" + author["@type"] + ") : ") if author_type : author["@type"] = author_type author["email"] = raw_input("email : ") author["url"] = raw_input("url : ") author["sameAs"] = author["url"] author["sameAs"] = raw_input("same as : ") app_json["author"] = author print json.dumps(app_json, indent=2) confirm = raw_input("confirm (yes) : ") if confirm.lower() != "no" : break
The next part of the script asks for various inputs from the developer which he or she can either provide or skip, if an input skipped then the default value is used. However for some parameters like author name, url etc, no default value is used, the developer must provide those values later by editing the app.json file. Once all the inputs are taken, the script outputs the json object created, if the output is alright then the developer may press enter and the process will continue, otherwise the developer may enter ‘no’ and once again the script will start taking inputs.
Once the final app_json is created, next part is creating the actual directory structure with the default files. This is done as shown below.
os.mkdir(app_json["name"]) os.chdir(app_json["name"]) os.mkdir("css") os.mkdir("js") index_html = open("index.html", 'w') index_html.write("<!-- entry point of the app, on launching the appliccation" + "this page will be displayed -->") index_html.close() app_json_file = open("app.json", 'w') app_json_file.write(json.dumps(app_json, indent=2)) app_json_file.close() get_started_md = open("getStarted.md", 'w') get_started_md.write("<!-- getting started with the app -->") get_started_md.close() app_use_md = open("appUse.md", 'w') app_use_md.write("<!-- use of this app -->") app_use_md.close() others_md = open("others.md", 'w') others_md.write("<!-- other relevant information about this app -->") others_md.close() style_css = open("css/style.css", 'w') style_css.write("/* css to style the app */") style_css.close() script_js = open("js/script.js", 'w') script_js.write("// javascript logic for the app") script_js.close() print "done"
A new directory is created in the root of the repository, the name of the directory is same as the app name. Python’s os module is used to create the directory and subdirectories. Inside the directory, app.json file is created in which app_json dictionary is dumped as a json object.
After this the other default files are created. These include index.html, js/script.js, css/style.css
getStarted.md, appUse.md and others.md. After everything is done script exits and the developer is all set to start with his development.
Finally loklakinit.sh is used to call loklakinit.py
#!/usr/bin/env bash python bin/loklakinit.py
This allows users to initiate the script by just executing bin/loklakinit.sh from root of the repository. The entire source code for this script can be found here (python code) and here (bash script)