using google maps api with python - python

I'm following this guide in starting with gmaps api and python;
import gmaps
import gmaps.datasets
import pandas as pd
def func():
# Use google maps api
gmaps.configure(api_key='MY_API_KEY') # Fill in with your API key
# Get the dataset
earthquake_df = gmaps.datasets.load_dataset_as_df('earthquakes')
# Get the locations from the data set
locations = earthquake_df[['latitude', 'longitude']]
# Get the magnitude from the data
weights = earthquake_df['magnitude']
# Set up your map
fig = gmaps.figure()
fig.add_layer(gmaps.heatmap_layer(locations, weights=weights))
return fig
func()
been using this code from the guide (both on pychram and jupyter notebooks) but when i run the code (pychram/jupyter/terminal) I don't get the output map like in the guide.
just a nice old-fashion
Process finished with exit code 0

Check if it is enabled for jupyter first by running jupyter nbextension list.
If you do not see jupyter-gmaps/extension enabled then you need to run this line in terminal jupyter nbextension enable --py gmaps and then run a new jupyter notebook.

Related

the algorithm don't display the mesh

I am using Jupyter in a Conda environment:
import igl
import meshplot as mp
import numpy as np
v, f = igl.read_triangle_mesh("./earth.ply")
k = igl.gaussian_curvature(v, f)
mp.plot(v, f, k, return_plot = True)
OUTPUT:
<meshplot.Viewer.Viewer at 0x1b53eb03fa0>
it is not displaying the mesh. it just outputs the location it stored in memory. Please help me.
It seems that you have your meshplot.rendertype set to "OFFLINE".
If you are using this code in a jupyter notebook and want to display the mesh, then just switch rendertype to "JUPYTER", by executing mp.jupyter() somewhere before your plot() command.
If you are running the code as a normal python program, you can export this View object as a HTML frame using the View.to_html() method. Then you can insert this frame into a html file and view it in a browser.
You can check out the source code for switching rendertype here, how the mp.plot function works here. The View class with the to_html method is defined here.

Shap value plotting error on Databricks but works locally

I want to do a simple shap analysis and plot a shap.force_plot. I noticed that it works without any issues locally in a .ipynb file, but fails on Databricks with the following error message:
Visualization omitted, Javascript library not loaded!
Have you run `initjs()` in this notebook? If this notebook was from another user you must
also trust this notebook (File -> Trust notebook). If you are viewing this notebook on
github the Javascript has been stripped for security. If you are using JupyterLab this
error is because a JupyterLab extension has not yet been written.
Code:
import xgboost
import shap
shap.initjs()
X, y = shap.datasets.boston()
bst = xgboost.train({"learning_rate": 0.01}, xgboost.DMatrix(X, label=y), 100)
explainer = shap.TreeExplainer(bst)
shap_values = explainer.shap_values(X)
shap.force_plot(explainer.expected_value, shap_values[0,:], X.iloc[0,:])
Is there any way to get the image plotting work on Databricks?
Let's try slightly different (matplotlib=True):
import xgboost
import shap
X, y = shap.datasets.boston()
bst = xgboost.train({"learning_rate": 0.01}, xgboost.DMatrix(X, label=y), 100)
explainer = shap.TreeExplainer(bst)
shap_values = explainer.shap_values(X)
shap.force_plot(
explainer.expected_value,
shap_values[0,:],
X.iloc[0,:],
matplotlib=True # <--
)

Tqdm status bar for generating a Folium map

I am generating a map inside Google Colab using the Folium python library and would like to show a progress bar as the code is working on spitting out a map rendering.
I'm fairly new to programming, but I gather that tqdm can generate a status bar over an iterable. I don't necessarily have an iterable built into my code, but I'm wondering how to 'wrap' my code in a tqdm function to show a status bar as I wait for the various !pip install, import, and folium functions to run. Unfortunately, Colab forces me to run the !pip install statements each time in order to work properly.
Here is my code for generating a fairly simple map, although the status bar doesn't behave as expected:
for i in tqdm(range(1), desc = 'Generating Map'):
!pip install geopandas &> /dev/null
!pip install branca &> /dev/null
import folium
from folium.plugins import HeatMap
from folium.features import GeoJsonTooltip
import geopandas as gpd
import pandas as pd
import os
from branca.element import Figure
import glob
from google.colab import drive
m=folium.Map(location=[40.43912857098136, -79.94770150049476],tiles=None)
folium.TileLayer(
tiles = 'https://mt1.google.com/vt/lyrs=m&x={x}&y={y}&z={z}',
attr = 'Google',
name = 'Google Maps',
overlay = False,
control = True,
zoom_start = 12,
min_zoom = 8,
max_zoom = 20,
opacity = 1
).add_to(m)
m
I'm installing geopandas and branca to use when my map becomes more complex. This is a fairly slimmed-down version of my entire code.
Problem is, the status bar doesn't begin to tick forward until the very last section of the code, when it zooms ahead to 100%. It just blinks for the first few seconds when I run this. I'd like the status bar to begin immediately upon running the very first !pip install command.
This particular chunk of code only takes a few seconds to run, but later I'll want to add a choropleth map and other shapefile layers to the map that will undoubtedly slow down the time it takes to execute. Hence, a progress bar to show the user a status would be quite helpful.

qiskit plots not showing up in spyder console

System Informations
Qiskit version: 0.17.0
Python version: 3.7.7
Operating system: Windows 10 home x64
What is the current behavior?
I am using spyder 4.1.1 on Anaconda and any time I try to plot data it does not show up. The code runs with no errors but the plot it self does not appear anywhere.
Steps to reproduce the problem
Running the code listed below which is from the IBMQ website:
import numpy
import qiskit as qc
from qiskit import QuantumCircuit, execute, Aer
import matplotlib
from qiskit.visualization import plot_state_city
circ = qc.QuantumCircuit(3)
circ.h(0)
circ.cx(0,1)
circ.cx(0,2)
print(circ.draw())
backend = Aer.get_backend('statevector_simulator')
job = execute(circ, backend)
result = job.result()
outputstate = result.get_statevector(circ, decimals=3)
print(outputstate)
plot_state_city(outputstate)
What is the expected behavior?
for the plot state city plot to show up in the console or somewhere else
Suggested solutions
I tried using both matplotlib.pylot.show() and matplotlib.pyplot.draw()
Try follow the instructions under "Spyder plots in separate windows" here. Then you can also call circ.draw(output='mpl') to draw the circuit in a window. Hope this helps.
To print your circuit, you have to type print(name of the circuit) so in your case type print(circ).
This may be because the plots don't show in spyder by default. If you run this code in a Jupyter Notebook the plots should show up just fine!
I generally use pycharm ide for running the qiskit code. I think, spyder should also work the same.
Please modify your code as below. I hope, it will work for you.
import numpy
import qiskit as qc
from qiskit import QuantumCircuit, execute, Aer
import matplotlib
from qiskit.visualization import plot_state_city
import matplotlib.pyplot as plt
circ = qc.QuantumCircuit(3)
circ.h(0)
circ.cx(0,1)
circ.cx(0,2)
circ.draw(output='mpl')
backend = Aer.get_backend('statevector_simulator')
job = execute(circ, backend)
result = job.result()
outputstate = result.get_statevector(circ, decimals=3)
print(outputstate)
plot_state_city(outputstate)
plt.show()
1st: I changed the preferences of spyder: Tools ==> Preferences ==> ipython console ==> Graphics ==> Graphics backend ==> Automatic.
2nd: then I have tried the option mentioned in the answer above, with minor modification is that by writing 'mpl' only between the brackets after the word "draw" to have the code be circ.draw('mpl'), and things worked fine.

How to load the Odoo Environment into a Jupyter Notebook?

Odoo (ERP)
Odoo is an all-in-one management software that offers a range of business applications that form a complete suite of enterprise management applications targeting companies of all sizes. Odoo is an all-in-one business software including CRM, website/e-commerce, billing, accounting, manufacturing, warehouse - and project management, and inventory.
The Community version is the open source version while the Enterprise version supplements the Community edition with commercial features and services.
Terminal
Usually I launch a Python Terminal with the Odoo environment loaded like this:
/odoo_path/odoo-bin --addons-path=/addons_path shell --config=/config_path/.odoo.conf -d database_name
Inside the console I can call to self, and the environment variable is available (self.env). And I can run some commands over the connected database. If I want to do some fast tests this is very useful. But it is kind of inconvinient if I want to run some small scripts or test some code snippets. Jupyter Notebooks and Jupyter Lab are very good tools for that.
Jupyter
Jupyter is a project which produces browser-based interactive environments for programming, mathematics, and data science. It supports a number of languages via plugins ("kernels"), such as Python, Ruby, Haskell, R, Scala and Julia.
Jupyter Notebook is the traditional and most stable application. JupyterLab has a new interface and is more suitable for working with larger projects consisting of multiple files. JupyterLab is in beta as of February 2018.
(source: windows.net)
Requests
What I want to achieve is load the Odoo environment into a Jupyter Notebook and work with these code snippets instead of the simple Python Terminal.
So I am wondering if anyone knows if this is doable or which would be a good place to start. Is there a better alternative? Is this a silly idea?
Step 1. install jupyter in your odoo environment
pip install jupyter
Step 2.
Start odoo shell and open jupyter from the python prompt
>>
from notebook.notebookapp import NotebookApp
import tornado.ioloop
app = NotebookApp()
app.initialize(["--ip=0.0.0.0"])
tornado.ioloop.IOLoop.current().start()
Step 3.
Get the token by sending CTRL+C to the python console
You see something like
http://(<hostname> or 127.0.0.1):8888/?token=9d150bd69e908df9a5e30157b530624536fe0c84d8804f17
Step 4.
Open your browser
http://localhost:8888/?token=9d150bd69e908df9a5e30157b530624536fe0c84d8804f17
Now you are in the Jupyter console or web interface
To connect to odoo you need to set the system path and then connect
import sys
sys.path[0:0] = [
'/vagrant/odoo/py3o.template',
'/vagrant/odoo/openupgradelib',
'/vagrant/odoo/anybox.recipe.odoo',
'/vagrant/odoo/pyusb',
'/vagrant/odoo/parts/odoo',
'/vagrant/odoo/eggs/gevent-1.1.2-py2.7-linux-x86_64.egg',
'/vagrant/odoo/eggs/Unidecode-1.1.1-py2.7.egg',
'/vagrant/odoo/eggs/py3o.formats-0.3-py2.7.egg',
'/vagrant/odoo/eggs/isoweek-1.3.3-py2.7.egg',
'/vagrant/odoo/eggs/PyPDF2-1.26.0-py2.7.egg',
'/vagrant/odoo/eggs/simplejson-3.17.2-py2.7-linux-x86_64.egg',
'/vagrant/odoo/eggs/xlrd-1.2.0-py2.7.egg',
'/vagrant/odoo/eggs/openpyxl-2.4.9-py2.7.egg',
'/vagrant/odoo/eggs/vcrpy-3.0.0-py2.7.egg',
'/vagrant/odoo/eggs/py2_ipaddress-3.4.2-py2.7.egg',
'/vagrant/odoo/eggs/Babel-2.8.0-py2.7.egg',
'/vagrant/odoo/eggs/decorator-4.4.2-py2.7.egg',
'/vagrant/odoo/eggs/docutils-0.16-py2.7.egg',
'/vagrant/odoo/eggs/feedparser-5.2.1-py2.7.egg',
'/vagrant/odoo/eggs/Jinja2-2.10.1-py2.7.egg',
'/vagrant/odoo/eggs/lxml-4.5.2-py2.7-linux-x86_64.egg',
'/vagrant/odoo/eggs/Mako-1.1.3-py2.7.egg',
'/vagrant/odoo/eggs/mock-2.0.0-py2.7.egg',
'/vagrant/odoo/eggs/ofxparse-0.20-py2.7.egg',
'/vagrant/odoo/eggs/passlib-1.7.4-py2.7.egg',
'/vagrant/odoo/eggs/Pillow-4.1.1-py2.7-linux-x86_64.egg',
'/vagrant/odoo/eggs/psutil-2.2.1-py2.7-linux-x86_64.egg',
'/vagrant/odoo/eggs/psycogreen-1.0.2-py2.7.egg',
'/vagrant/odoo/eggs/psycopg2-2.8.6-py2.7-linux-x86_64.egg',
'/vagrant/odoo/eggs/Python_Chart-1.39-py2.7.egg',
'/vagrant/odoo/eggs/pydot-1.4.1-py2.7.egg',
'/vagrant/odoo/eggs/pyparsing-2.4.7-py2.7.egg',
'/vagrant/odoo/eggs/pyPdf-1.13-py2.7.egg',
'/vagrant/odoo/eggs/pyserial-3.4-py2.7.egg',
'/vagrant/odoo/eggs/python_dateutil-2.8.1-py2.7.egg',
'/vagrant/odoo/eggs/python_ldap-3.3.1-py2.7-linux-x86_64.egg',
'/vagrant/odoo/eggs/python_openid-2.2.5-py2.7.egg',
'/vagrant/odoo/eggs/pytz-2020.1-py2.7.egg',
'/vagrant/odoo/eggs/PyYAML-5.3.1-py2.7-linux-x86_64.egg',
'/vagrant/odoo/eggs/qrcode-6.1-py2.7.egg',
'/vagrant/odoo/eggs/reportlab-2.7-py2.7-linux-x86_64.egg',
'/vagrant/odoo/eggs/requests-2.24.0-py2.7.egg',
'/vagrant/odoo/eggs/suds_jurko-0.6-py2.7.egg',
'/vagrant/odoo/eggs/vatnumber-1.2-py2.7.egg',
'/vagrant/odoo/eggs/vobject-0.9.6.1-py2.7.egg',
'/vagrant/odoo/eggs/Werkzeug-0.11.11-py2.7.egg',
'/vagrant/odoo/eggs/XlsxWriter-1.3.6-py2.7.egg',
'/vagrant/odoo/eggs/xlwt-1.3.0-py2.7.egg',
'/vagrant/odoo/eggs/python_stdnum-1.14-py2.7.egg',
'/vagrant/odoo/eggs/urllib3-1.25.10-py2.7.egg',
'/vagrant/odoo/eggs/idna-2.10-py2.7.egg',
'/vagrant/odoo/eggs/chardet-3.0.4-py2.7.egg',
'/vagrant/odoo/eggs/certifi-2020.6.20-py2.7.egg',
'/vagrant/odoo/eggs/six-1.15.0-py2.7.egg',
'/vagrant/odoo/eggs/pyasn1_modules-0.2.8-py2.7.egg',
'/vagrant/odoo/eggs/pyasn1-0.4.8-py2.7.egg',
'/vagrant/odoo/eggs/olefile-0.46-py2.7.egg',
'/vagrant/odoo/eggs/beautifulsoup4-4.9.3-py2.7.egg',
'/vagrant/odoo/eggs/pbr-5.5.0-py2.7.egg',
'/vagrant/odoo/eggs/funcsigs-1.0.2-py2.7.egg',
'/vagrant/odoo/eggs/MarkupSafe-1.1.1-py2.7-linux-x86_64.egg',
'/vagrant/odoo/eggs/greenlet-0.4.17-py2.7-linux-x86_64.egg',
'/vagrant/odoo/eggs/zc.recipe.egg-2.0.7-py2.7.egg',
'/vagrant/odoo/eggs/wrapt-1.12.1-py2.7-linux-x86_64.egg',
'/vagrant/odoo/eggs/contextlib2-0.6.0.post1-py2.7.egg',
'/vagrant/odoo/eggs/et_xmlfile-1.0.1-py2.7.egg',
'/vagrant/odoo/eggs/jdcal-1.4.1-py2.7.egg',
'/vagrant/odoo/eggs/pyjon.utils-0.7-py2.7.egg',
'/vagrant/odoo/eggs/Genshi-0.7.3-py2.7-linux-x86_64.egg',
'/vagrant/odoo/eggs/cssselect-1.1.0-py2.7.egg',
'/vagrant/odoo/eggs/soupsieve-1.9.5-py2.7.egg',
'/vagrant/odoo/eggs/backports.functools_lru_cache-1.6.1-py2.7.egg',
'/home/vagrant/venv/lib/python2.7/site-packages',
]
from anybox.recipe.odoo.runtime.session import Session
%%capture
session = Session('/vagrant/odoo/etc/odoo.cfg', '/vagrant/odoo')
%%capture
session.open(db='odootest')
User = session.env['res.users']
user = User.browse(1)
print(user.name)
Administrator
Please find attached a minimal odoo buildout script
[buildout]
parts = odoo
versions = versions
# Mr Developer Extension
extensions = mr.developer
auto-checkout = *
always-checkout = force
[sources]
# Sources that Mr Developer needs to check out
anybox.recipe.odoo = git https://github.com/anybox/anybox.recipe.odoo branch=master
openupgradelib = git https://github.com/OCA/openupgradelib branch=master
odoocrpc = git https://github.com/OCA/odoorpc branch=master
[odoo]
release = 14.0
apply-requirements-file = True
recipe = anybox.recipe.odoo:server
eggs =
gevent
odoorpc
openupgradelib
unidecode
inouk.recipe.odoo_cmd
odoo_scripts =
command-line-options=-d
odoo_cmd=odoo-bin
version = git https://github.com/odoo/odoo.git odoo ${odoo:release}
# This directive will nuke local changes, but without it, a branch with merges
# cannot be updated if the main branch has progressed. This will break
# rebuilds. Use this in production and testing, not in development.
vcs-clear-retry = True
git-warn-sha-pins = False
addons =
git https://github.com/oca/web parts/web ${odoo:release}
git https://github.com/oca/server-tools parts/server-tools ${odoo:release}
merges =
# Config options
options.http_enable = True
options.http_port = 8169
options.lang = nl_NL
options.limit_time_cpu = 36000
options.limit_time_real = 72000
options.log_handler = :DEBUG,werkzeug:CRITICAL,openerp.service.server:INFO,PIL:INFO
options.log_level = debug
options.logrotate = True
options.longpolling_port = 8170
# options.proxy_mode = True
# options.without_demo = False
options.workers = 4
[versions]
xlwt = 1.3.0

Categories