Categories
JavaScript Answers

How to get visitor’s location using geolocation with JavaScript?

Sometimes, we want to get visitor’s location using geolocation with JavaScript.

In this article, we’ll look at how to get visitor’s location using geolocation with JavaScript.

How to get visitor’s location using geolocation with JavaScript?

To get visitor’s location using geolocation with JavaScript, we can use the ipregistry API.

For instance, we write

const response = await fetch("https://api.ipregistry.co/?key=tryout");
const payload = await response.json();
console.log(payload.location.country.name, payload.location.city);

in an async function to make a GET request to https://api.ipregistry.co/?key=tryout with fetch.

Then we get the location data from the location property of the response.

Conclusion

To get visitor’s location using geolocation with JavaScript, we can use the ipregistry API.

Categories
Python Answers

How to pretty print nested dictionaries with Python?

Sometimes, we want to pretty print nested dictionaries with Python.

In this article, we’ll look at how to pretty print nested dictionaries with Python.

How to pretty print nested dictionaries with Python?

To pretty print nested dictionaries with Python, we call json.dumps with the indent argument.

For instance, we write

import json

print(
    json.dumps(
        {"a": 2, "b": {"x": 3, "y": {"t1": 4, "t2": 5}}}, sort_keys=True, indent=4
    )
)

to print the JSON string returned by json.dumps.

We call it with indent set to 4 to indent by 4 spaces in each level.

Conclusion

To pretty print nested dictionaries with Python, we call json.dumps with the indent argument.

Categories
Python Answers

How to iterate over model instance field names and values in template with Python Django?

Sometimes, we want to iterate over model instance field names and values in template with Python Django.

In this article, we’ll look at how to iterate over model instance field names and values in template with Python Django.

How to iterate over model instance field names and values in template with Python Django?

To iterate over model instance field names and values in template with Python Django, we can use a for loop.

For instance, we write

from django.forms.models import model_to_dict


def show(request, object_id):
    object = FooForm(data=model_to_dict(Foo.objects.get(pk=object_id)))
    return render_to_response("foo/foo_detail.html", {"object": object})

in the show view that passes the object object to the foo_detail.html template.

Then in the template, we write

{% for field in object %}
    <li><b>{{ field.label }}:</b> {{ field.data }}</li>
{% endfor %}

to loop through the object attributes and get the attribute name from label and the attribute value from data.

Conclusion

To iterate over model instance field names and values in template with Python Django, we can use a for loop.

Categories
Python Answers

How to get list from Python Pandas dataframe column or row?

Sometimes, we want to get list from Python Pandas dataframe column or row.

In this article, we’ll look at how to get list from Python Pandas dataframe column or row.

How to get list from Python Pandas dataframe column or row?

To get list from Python Pandas dataframe column or row, we use the tolist method.

For instance, we write

import pandas as pd

data_dict = {
    "one": pd.Series([1, 2, 3], index=["a", "b", "c"]),
    "two": pd.Series([1, 2, 3, 4], index=["a", "b", "c", "d"]),
}

df = pd.DataFrame(data_dict)
col_one_list = df["one"].tolist()

to create the df data frame from the data_dict dict.

Then we get column one’s values with df['one'] and call tolist to return the values in the series as a list.

Conclusion

To get list from Python Pandas dataframe column or row, we use the tolist method.

Categories
Python Answers

How to execute a file within the Python interpreter?

Sometimes, we want to execute a file within the Python interpreter.

In this article, we’ll look at how to execute a file within the Python interpreter.

How to execute a file within the Python interpreter?

To execute a file within the Python interpreter, we can run python with the file name of the file.

In the interactive prompt, we can use exec.

For instance, we run

python filename.py

to run filename.py from the shell.

When we’re in the REPL, we run

exec(open("filename.py").read())

to run filename.py by opening it with open and then call read to read the code.

Then we call exec with the returned code to run it.

Conclusion

To execute a file within the Python interpreter, we can run python with the file name of the file.

In the interactive prompt, we can use exec.