How can I do a Static Image Export on Google Colab - python

I'm trying to export an image I've created with plotly as a .svg file.
This is the code I'm running to export it:
fig.write_image("/content/fig1.svg")
This is the error I'm getting:
I've entered in the github page of Orca and tried to use the installation codes they suggest but neither one worked.

You can install orca with the following code. (a library I wrote)
!pip install kora -q
import kora.install.orca
Then it can write_image
import plotly.graph_objects as go
fig = go.Figure( go.Scatter(x=[1,2,3], y=[1,3,2] ) )
fig.write_image("image.png")
from IPython.display import Image
Image("image.png")
If you prefer to install orca yourself
import os
from urllib.request import urlretrieve
url = "https://github.com/plotly/orca/releases/download/v1.2.1/orca-1.2.1-x86_64.AppImage"
orca = '/usr/local/bin/orca'
urlretrieve(url, orca)
os.chmod(orca, 0o755)
os.system("apt install xvfb libgconf-2-4")

Related

ModuleError: No module named pptx found

I am writing a python code for merging ppts. It takes the location of the 2 ppts, merges them and puts the merged ppt formed in the folder given by user. The code used is:
import sys
from pptx import Presentation
#import Aspose.Words.License
#import aspose.slides as a_slides
#import os
#import win32com.client
def merge_powerpoint_ppts(pres_loc1, pres_loc2, output_loc):
p1 = open(pres_loc1)
pres1 = Presentation(p1)
p2 = open(pres_loc2)
pres2 = Presentation(p2)
for slide in pres2.slides:
for lide in pres1.slides:
if slide.shape.title.text == lide.shape.title.text:
pres1.slides.add_Clone(slide)
pres1.save(output_loc)
p1.close()
p2.close()
When I try to debug the code, I get the following:
[Error][1]
[1]: https://i.stack.imgur.com/yID2l.png
I have already installed the module pptx on my system and it is updated, but I am still getting this error.
First if you have a folder named Presentation or pptx change it cause this could happen because of naming confusion of files or folder and python modules
Secondly make sure you use the correct python interpreter or env where you install pptx in in
last Option uninstall pptx and write the following command
conda install -c conda-forge python-pptx

ModuleNotFoundError: No module named 'google' while using google-cloud-vision api

i ran the following code
import io
import os
# Imports the Google Cloud client library
from google.cloud import vision
# Instantiates a client
client = vision.ImageAnnotatorClient()
# The name of the image file to annotate
file_name = os.path.abspath('resources/wakeupcat.jpg')
# Loads the image into memory
with io.open(file_name, 'rb') as image_file:
content = image_file.read()
image = vision.Image(content=content)
# Performs label detection on the image file
response = client.label_detection(image=image)
labels = response.label_annotations
print('Labels:')
for label in labels:
print(label.description)
the error i get is the following
Traceback (most recent call last):
File "F:\coding\pythonfolder\main.py", line 4, in <module>
from google.cloud import vision
ModuleNotFoundError: No module named 'google'
the library is installed is the following
pip install google-cloud-vision
I am fairly new to the google vision api so maybe there's some point I am missing
kindly let me know what the issue is, I tried pip install --upgrade google-cloud-vision as well but does not seems to work
my python version is Python 3.10.0
kindly let me know if there's any issue or missing information in my question
thank you!!
I'm pretty sure you messed up with the venv, since it worked fine for me. Try using PyCharm (if you don't already do) and setup a venv, or install virtualenv and launch these commands inside your project folder:
virtualenv venv
source venv/bin/activate
pip install google-cloud-vision
python your_file.py
deactivate
Last one is to properly exit the venv

wget not found in Jupiter notebook to download geojson file from url

Im trying to download and read a geojson file from url to use it latter to create a folium map, I already install wget on mac using brew.
when running the code I get this
# Download and store a geojson file of Indiana containig AGEB boundaries
import wget
import geojson
!wget https://github.com/Alexrendon/Indianapolis-data/blob/main/Indiana_censustracts.geojson
census_tract = r'Indiana_censustracts.geojson'
print("geojson ready!")
OUTPUT
zsh:1: command not found: wget
Solution was to use !curl
import wget
import geojson
!curl -O https://github.com/Alexrendon/Indianapolis-data/blob/main/Indiana_censustracts.geojson
census_tract = r'Indiana_censustracts.geojson'
print("geojson ready!")
Your code
# Download and store a geojson file of Indiana containig AGEB boundaries
import wget
import geojson
!wget https://github.com/Alexrendon/Indianapolis-data/blob/main/Indiana_censustracts.geojson
census_tract = r'Indiana_censustracts.geojson'
print("geojson ready!")
looks like you are confusing wget python module available at PyPI and GNU Wget command line tool. If you want just to downlad file, neither is required as there exist urlretrieve inside urllib.request which is part of python standard library. Consider following simple example
import urllib.request
urllib.request.urlretrieve("https://www.example.com","example.html")
first argument is URL, second is filename
The error tells you that you don't have wget on your machine. To install it on Mac, just do
brew install wget

ImportError: `iplot` can only run inside an IPython Notebook

When I run the following code in PyCharm on a Mac:
import numpy as np
import pandas as pd
from subprocess import check_output
print(check_output(["ls", "../input"]).decode("utf8"))
import time
import copy
import numpy as np
import pandas as pd
import chainer
import chainer.functions as F
import chainer.links as L
from plotly import tools
from plotly.graph_objs import *
from plotly.offline import init_notebook_mode, iplot, iplot_mpl
init_notebook_mode()
data = pd.read_csv('../input/Data/Stocks/goog.us.txt')
data['Date'] = pd.to_datetime(data['Date'])
data = data.set_index('Date')
print(data.index.min(), data.index.max())
data.head()
There were some errors:
UserWarning: Accelerate has been detected as a NumPy backend library.
vecLib, which is a part of Accelerate, is known not to work correctly with Chainer.
We recommend using other BLAS libraries such as OpenBLAS.
For details of the issue, please see
https://docs.chainer.org/en/stable/tips.html#mnist-example-does-not-converge-in-cpu-mode-on-mac-os-x.
Please be aware that Mac OS X is not an officially supported OS.
''') # NOQA
Traceback (most recent call last):
File "/Users/yindeyong/Desktop/PythonProjects/pythonstock/DQNStcok.py", line 33, in <module>
init_notebook_mode()
File "/Users/yindeyong/Desktop/PythonProjects/envs/stockenv/lib/python3.6/site-packages/plotly/offline/offline.py", line 250, in init_notebook_mode
raise ImportError('`iplot` can only run inside an IPython Notebook.')
ImportError: `iplot` can only run inside an IPython Notebook.
Process finished with exit code 1
I'm new in Chainer and DQN. Could anyone help me edit this code to make it work? Thank you so much!
It seems like you're trying to run plotly interactive functions (i prefix) in a normal Python code (i.e. not IPython Notebook). iplot provides an interactive graph with which you can play inside the notebook.
I'd start with removing iplot import and replacing it with normal plot. Also, remove iplot_mpl and init_notebook_mode from your imports.
You can install 'ipython' in the virtual environment and then try running it directly from the terminal.
Activate the virtual environment
pip3 install ipython
python3 prog.py
Worked in my case.

Plotly R not working with Jupyter notebook

I want to incorporate Plotly graphs in my Jupyter notebook but I'm getting a 404 error when I try to display a graph:
According to this post
import sys; print(sys.executable)
Gives me this location for Python. I have the two Pandoc files in this directory as and all the files needed as mentioned on Plotly's page.
C:\Users\name\AppData\Local\Continuum\Anaconda3\python.exe
I have also tried reinstalling Anaconda and Pandoc but I'm still getting this error. Any thoughts?
Do you have a plotly account? You may need to login into it within your notebook with something similar to this:
import plotly
plotly.tools.set_config_file(plotly_domain='domain')
from plotly import __version__
from plotly.offline import download_plotlyjs, init_notebook_mode, iplot
import plotly.plotly as py
print __version__ # requires version >= 1.9.0
import plotly.graph_objs as go
init_notebook_mode() # run at the start of every ipython notebook to use plotly.offline
# this injects the plotly.js source files into the notebook
py.sign_in(username='xxxx', api_key='xxxx')
I have the same issue in MacOS. In the console, it can be seen that it's looking for .html.embed but the file which is created is .html.
I have tried,
1) reinstalling Pandocs
2) Had to reinstall Cairo as package pbdZMQ was not getting installing properly.
This is the workaround I did,
p <- plot_ly(ds, x = x, y = y, mode = "markers", group = group, size = size)
htmlwidgets::saveWidget(as.widget(p), "index.html")
rawHTML <- paste(readLines("index.html"), collapse="\n")
display_html(rawHTML)
This is not the best solution, but for the time being this works for me.

Categories