Python Bokeh FileInput Widget - python-3.x

I want to upload a .txt file with two column datas using the bokeh FileInput widget and then plot the data interactively. Can someone provide me a minimal example?
Thanks a lot.

There is an example on importing files via the server directory structure and papaparse here:
Upload a CSV file and read it in Bokeh Web app
This was made a long time ago, before the FileInput widget was officially included with the Bokeh 1.3.0 distribution. Now it should work with this new widget however i cannot find the documentation on how to add a server callback to it.
After testing i come to this use of the new FileInput widget:
from bokeh.io import curdoc
from bokeh.models.widgets import FileInput
def upload_fit_data(attr, old, new):
print("fit data upload succeeded")
print(file_input.value)
file_input = FileInput(accept=".csv,.json,.txt")
file_input.on_change('value', upload_fit_data)
doc=curdoc()
doc.add_root(file_input)
This will give you the file data as a base64 encoded string (file_input.data). I leave it up to you to convert the base64 string to what you want and plot the data.

Related

convert html to png using gmplot in Python

I am using gmplot in Python 3.8 to generate a layer of polygons on top of a satellite google maps layer. The map is saved to .html format but I would like to be able to convert the .html file to .png format to embed it in a pdf created in Python at a later stage (that will contain other elements, such as text and other images).
I generate the map using standard code as described in the gmplot tutorial:
import gmplot
latitude_list = [ 17.4567417, 17.5587901, 17.6245545]
longitude_list = [ 78.2913637, 78.007699, 77.9266135 ]
gmap = gmplot.GoogleMapPlotter(17.438139, 78.3936413, 11)
gmap.polygon(latitude_list, longitude_list, color = 'cornflowerblue')
gmap.draw("path_to_html")
I have checked different posts to get a solution, including this one and this one. From one of these posts, I have managed to get the following snippet of code:
import time
from selenium import webdriver
import chromedriver_binary # adds chromedriver binary to path
driver = webdriver.Chrome()
driver.get("local_url_of_html_file")
time.sleep(3)
driver.save_screenshot('map.png')
driver.quit()
It appears this code takes a screenshot of the html but I was wondering if there is any in-built function in gmplot to do this in a more straightforward way or other packages like bokeh.

Bokeh how to load a file with its directory?

I am trying to get the file to work in bokeh. When using the input widget, it only gives me the filename. How to make a file name with a directory, when opening the file there were no errors?
csvfile = FileInput() # csvfile = ('C:/matlab0012.csv')
csvopen = myfun(csvfile) # myfun-my function that creates graphs from data from a file
#bigreddot is right. you cannot get full path. but you can reach selected file. However, you have to decode it first.
minimal example :
from pybase64 import b64decode
def get_file(attr, old, new):
file = io.BytesIO(b64decode(new))
new_data = pd.read_csv(file) # pandas or just use open.
file_input = FileInput(name="fileinput", accept="<.csv>")
file_input.on_change('value', get_file)
This is impossible. For security reasons, browsers will not provide the full path. They will only provide the filename and the file contents from the file that was requested.
Assuming those is a Bokeh server application, you can only respond to a file selection with an on_change callback that you add to the value property of the input widget.
If this is standalone output (not Bokeh server) then you can only respond with a JavaScript js_on_change callback since the Bokeh content displayed in the browser is not connected to any Python process.
In either case, all that the browser will provide is the file contents (which Bokeh stores as base64 encoded strings in the value property).

Trying to pass a base_64 encoded image to google authentication

So i am working on a small project(a restful service so json format which is not mentioned in the code) in which the code accepts base_64 image data and decodes it to from an image ,i'm able to convert it back to image but i am not able to use google vision(googel ocr) on the image to extract the text . The only part that isn't working is the following block of code:
from flask import Flask,request,jsonify
import os,io,re,glob,base64
from google.cloud import vision
from google.cloud.vision import types
from PIL import Image
app = Flask(__name__)
os.environ['GOOGLE_APPLICATION_CREDENTIALS']=r'date_scanner.json'
#app.route('/jason_example',methods=['POST'])
def jason_example():
req_data=request.get_json()
base_64_image_content=req_data['imgcnt']
#the issue starts from here
image = base64.b64decode(base_64_image_content)
image=Image.open(io.BytesIO(image))
image=vision.types.Image(content=content)
response=client.text_detection(image=image)
texts=response.text_annotations`
enter code here
No need to use Image.open which I think is a PIL method anyway. You should be able to decode this straight to a byte string with base64.decodebytes, as outlined in this answer,
The code should look like:
# the issue starts from here
image_bytes = base64.decodebytes(base_64_image_content)
image = vision.types.Image(content=image_bytes)
response=client.text_detection(image=image)
texts=response.text_annotations

Importing scripts into a notebook in IBM WATSON STUDIO

I am doing PCA on CIFAR 10 image on IBM WATSON Studio Free version so I uploaded the python file for downloading the CIFAR10 on the studio
pic below.
But when I trying to import cache the following error is showing.
pic below-
After spending some time on google I find a solution but I can't understand it.
link
https://dataplatform.cloud.ibm.com/docs/content/wsj/analyze-data/add-script-to-notebook.html
the solution is as follows:-
Click the Add Data icon (Shows the Add Data icon), and then browse the script file or drag it into your notebook sidebar.
Click in an empty code cell in your notebook and then click the Insert to code link below the file. Take the returned string, and write to a file in the file system that comes with the runtime session.
To import the classes to access the methods in a script in your notebook, use the following command:
For Python:
from <python file name> import <class name>
I can't understand this line
` and write to a file in the file system that comes with the runtime session.``
Where can I find the file that comes with runtime session? Where is the file system located?
Can anyone plz help me in this with the details where to find that file
You have the import error because the script that you are trying to import is not available in your Python runtime's local filesystem. The files (cache.py, cifar10.py, etc.) that you uploaded are uploaded to the object storage bucket associated with the Watson Studio project. To use those files you need to make them available to the Python runtime for example by downloading the script to the runtimes local filesystem.
UPDATE: In the meanwhile there is an option to directly insert the StreamingBody objects. This will also have all the required credentials included. You can skip to writing it to a file in the local runtime filesystem section of this answer if you are using insert StreamingBody object option.
Or,
You can use the code snippet below to read the script in a StreamingBody object:
import types
import pandas as pd
from botocore.client import Config
import ibm_boto3
def __iter__(self): return 0
os_client= ibm_boto3.client(service_name='s3',
ibm_api_key_id='<IBM_API_KEY_ID>',
ibm_auth_endpoint="<IBM_AUTH_ENDPOINT>",
config=Config(signature_version='oauth'),
endpoint_url='<ENDPOINT>')
# Your data file was loaded into a botocore.response.StreamingBody object.
# Please read the documentation of ibm_boto3 and pandas to learn more about the possibilities to load the data.
# ibm_boto3 documentation: https://ibm.github.io/ibm-cos-sdk-python/
# pandas documentation: http://pandas.pydata.org/
streaming_body_1 = os_client.get_object(Bucket='<BUCKET>', Key='cifar.py')['Body']
# add missing __iter__ method, so pandas accepts body as file-like object
if not hasattr(streaming_body_1, "__iter__"): streaming_body_1.__iter__ = types.MethodType( __iter__, streaming_body_1 )
And then write it to a file in the local runtime filesystem.
f = open('cifar.py', 'wb')
f.write(streaming_body_1.read())
This opens a file with write access and calls the write method to write to the file. You should then be able to simply import the script.
import cifar
Note: You can get the credentials like IBM_API_KEY_ID for the file by clicking on the Insert credentials option on the drop-down menu for your file.
The instructions that op found miss one crucial line of code. I followed them and was able to import modules but wasn't able to use any functions or classes in those modules. This was fixed by closing the files after writing. This part in the instrucitons:
f = open('<myScript>.py', 'wb')
f.write(streaming_body_1.read())
should instead be (at least this works in my case):
f = open('<myScript>.py', 'wb')
f.write(streaming_body_1.read())
f.close()
Hopefully this helps someone.

Extracting title from pdf using pypdf2 not working

I'm trying to extract the title of PDF files using pyPDF2. The output is either none or a wrong title. I tried using PDFminer as well, still the same result. I tried using 3 different pdf files. Is there a better way to extract the title with better accuracy?
This is the code I used:
from PyPDF2 import PdfFileReader
def get_pdf_title(pdf_file_path):
pdf_reader = PdfFileReader(open(pdf_file_path, "rb"))
return pdf_reader.getDocumentInfo().title
title = get_pdf_title('C:/PythonPrograms/Test.pdf')
print(title)
Your code is working, at least for me on python 3.5.2. Check in the PDF properties that he indeed has a title.
PDF's title is part of its metadata, that needs to be set. It is not mandatory, not related to its content (other than by the will of the person writing it), nor with its filename.
If you use your snippet on a file with no title, it's output will be an empty string.

Resources