

#PRETTY PRINTED FLASK CHEAT SHEET CODE#
The above code generates a 16 character GUID. Fork mode pm2 start app.js -name my-api Name process Cluster mode pm2 start app.js -i 0 Will start maximum processes with LB depending on available CPUs pm2 start app.js -i max Same as above, but deprecated. We can use string operations to modify the GUID. By default, it produces a 32 character string. Uuid4() returns an object of type UUID but we can use str() to typecast it. FLASKRUNKEY - The key file for your cert.

FLASKRUNCERT - A certificate file for so your app can be run with HTTPS. Keyword arguments can be likened to dictionaries in that they map a value to a keyword. A keyword argument is preceded by a parameter and the assignment operator. FLASKRUNHOST - The host you want to bind your app to. Keyword arguments (or named arguments) are values that, when passed into a function, are identifiable by specific parameter names. We won’t be using the random module anymore, instead, we will use the uuid module import uuid FLASKRUNEXTRAFILES - A list of files that will be watched by the reloader in addition to the Python modules. Note: It will only work with dictionaries if the dictionary has an integer as a key. We can use the random library to chose a random element from an iterable. ''' Choosing a random element from an iterable So if we set the step size to 10, it will randomly choose from (0,10,20,30….) import random It is similar to the randint() function but it takes an additional parameter. We can adjust the start number and end number as needed. We use the randint() function and simply multiply the returned value with 10 import random Suppose, we want to generate a random multiple of 10.

It is inclusive,i.e randint() might return the start number or end number as well Generating a Random Multiple of an Integer The first argument is the start number and the second argument is the end number. This is probably the most popular use case of the random module. However, if you use the random module multiple times in a program, it will return a different random value for each call. Then what I would like it to do is redirect the user to a different page, but I can't get that to work. During each run, it’ll be the same random value. Using JS the Flask app is then sent a POST request, which in reality would add the data to a database or something, but I don't think it actually needs to do that for this example so it doesn't. Although the integer, float, element from the list will be random. We can set the seed for the random module. This is used to generate a random floating point between 0 and 1 import random It is a built-in module in Python Random() To use any of the functions below, we will first need to import the random module. Pick n random elements from an iterable.Choosing a random element from an iterable.Generating a Random Multiple of an Integer.Its most common use is to generate random integers. With open("jsonOutput.The Random Module is a pretty popular module in Python. With open("jsonOutput.json","w") as file:Ĭase2: Dictionary of Dictionaries to a List of Dictionaries import jsonĬase3: List of Dictionaries to a List of Lists import json In the below sections we will transform some JSON Data and store it in a new file Case1: List of Dictionaries to a Dictionary import json Parsing JSON stored as a list of lists import json Parsing JSON stored as a dictionary of dictionaries import jsonįor jsonName,jsonObject in ems(): Parsing JSON stored as a list of dictionaries import json In most cases, you will have to use some combination of the below cases. The following code snippets might be helpful. The syntax is the one we follow while traversing lists or dictionaries. The logic to parse JSON data will vary case by case. Parsing a JSON file depends on the format of the data, it could be a simple dictionary, list of dictionaries etc. If the file does exist, it will overwrite the data in the file import json If you are interested in updates about videos and articles I write, you can get those as well. If the file doesn’t exist, it will create a new file. Submit your email to get the Flask cheat sheet. Below are some commonly formated data you can convert to a JSON object. Print(json.dumps(data, indent=4, sort_keys=True)) # 2nd Way The second way is using the JSON library the print function import json There are a couple of ways we can do that import json The output is hard to read, let’s improve its formatting. I have truncated the data but it’s basically a list of objects with a color and its corresponding hex value. Let’s assume we have a JSON file with the following data [

In this article, we will be doing some common operations while working with JSON data in Python
