This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
How to extract keys from a JavaScript / JSON object in Python
How to extract keys from complex json Object in Python? We can loop through it, but there is a better way.
JSON Object and Keys in Python
Usually we use json module to parse json objects in Python.
import json
...
data = json.loads(source)
The data variable hold the parsed json object and it can be a list of dictionary values. So how do we extract those keys ? Just like JS, Python also provides the keys method which will extract a list of keys.
countries = list(data.keys())
This will extract all country keys and convert it into a Python list.
How to make an API call and fetch json data in Python
API allows programmers to share the data in an organization or network. Modern apps may use multiple API’s too. So do we access API data in Python apps ?
Requirements
URLLIB3
JSON
In Most case data is shared as JSON , so we need to use json package to read the data, and urllib3 library for access and make request .
Function for fetch data
It is wise to create function for fetching data from API , so that you can reuse the function to read data from another API.
Here is the Python function to read data from an API end point .
</p>
from pip._vendor import urllib3
import json
def apicall(url):
try:
http = urllib3.PoolManager()
weburl = http.request('GET', url)
data = weburl.data.decode('utf-8')
return data
except urllib3.exceptions:
print('Error')
return
<p>
Selenium is one of the powerful package available in Python , used for web automation , no doubt about that. In the last we learned how to use a web driver with webdriver manager package. In this we will learn how to use a headless mode of action in selenium webdriver.
Headless mode
What is headless mode ? In head less mode ,browser you automate work in the background and the work can be completed silently. So how to run selenium web driver in headless mode .
You need to create browser option and add –headless.
Add the option to webdriver
Chrome option
Create a chrome option as follows and add attch to the web driver.
How to solve the chromewebdriver / selenium webdriver error using python package wedriver_manager
When I started with Selenium in Python my biggest problem was the webdriver issue which I later found that there is an automatic download option in python using some special package.
The webdriver sometimes not compatible with the browser you are using. You have to manually find them from the official websites. This is messy part of selenium, I think.
We can get ride off this problem using webdriver_manager
first you need to install the Python package using pip
</p>
pip install webdriver_manager
<p>
Now you can use it in your project
</p>
from webdriver_manager.chrome import ChromeDriverManager
from webdriver_manager.firefox import GeckoDriverManager
driver = webdriver.Chrome(executable_path=ChromeDriverManager().install())
driver = webdriver.Firefox(executable_path=GeckoDriverManager().install())
<p>
The package will automatically check for compatible version and install it and no more worries about web driver and scrap the web with ease and peace.
How to pass a python list to JavaScript with ninja template in Flask web app
You can’t use Python list directly into JavaScript, JavaScript lislt/array is not compatible with Python or wise versa. So what to do.
Solution
I have a list of names, which I want pass to a JavaScript in a Flask ninja template. Ninja template allow use to safely convert the python list to Json string then we can pass it to the JavaScript function / just JavaScript
Suppose you have separate JavaScript function files for some functionality like drawing analytic plots and pie diagram, or some donut chart. So how do we pass value to those functions ?
You can create a JavaScript as above, in your current template, then in function create a script variable to use it as follows.
</p>
function init_echarts() {
var distNames=dn;
}
<p>
That’s all you need to know to embed some morris diagram to your flask app.
You should look at Morris JS , it is free and easy to use JavaScript charts library.
How to pass variable to other template using { % set % } in Python Flask web app
Flask is one of my Python frame work in which I build simple web application with ease and peace. Today we learn how to pass a variables to other templates in a Flask application.
Suppose your web app have a base template and a top nav bar template which included using
</p>
{% include "site_template/top_navigation.html" %}
<p>
and you want to pass a title or something else from a Index page to navigation bar where you want show up some text.
Using {% set %} you can create global variables.
The solution
We can use a set block in top of the index page as follows ,to pass variables to other sub templates
Create RapidAPI powered app with Python and request
This tutorial is similar to RapidAPI example found in the official blog, with one exception, this example is users request method instead of unirest package which is not available on RapidAPI blog.
RapidAPI
RapidAPI is a website which provides useful APIs (Application Programing Interface) like Yahoo Finance, Email Verification, Geolocation which can be used to add features to your websites and apps using variety of programming languages like Python, Java, .Net etc.
Yahoo Finance example
For creating application to show stock chart using YahooFinanceAPI and Python, we are go through the following
Create a Rapid API Account
Subscribe to Yahoo Finance to get the endpoint (Which is a URL for getting information)
Create methods to connect API and Process data
Draw Plot
Install requirements
Install required packages using pip. I suggest using Python virtual environment.