How to fix Folium Popup size in Folium 0.12 - python

I am creating a Folium map with markers and popups. At the moment they are just static strings, however when I load the map the popup window only shows about 2 words before it moves to a new line. How do I get it to automatically resize so that it will appear on 1 line? The training course I am taking does not have this issue, but the instructor is using Folium 0.3.
Here is the code I am using. I am running Python 3.9.7 using VS Code.
import folium
map = folium.Map(location=[38.58, -99.09], zoom_start=6, tiles = "Stamen Terrain")
map.add_child(folium.Marker(location= [38.2,-99.1], popup="Hi this is a marker", icon=folium.Icon(color='green')))
map.save("Map1.html")

You can put the text of the popup inside a folium.Popup() object and specify the max_width parameter to 100%.
Extract from the Folium documentation :
max_width (int for pixels or text for percentages, default '100%') – The maximal width of the popup.
You can modify your code that way :
map.add_child(
folium.Marker(
location= [38.2,-99.1],
popup=folium.Popup("Hi this is a marker", max_width="100"),
icon=folium.Icon(color='green')
)
)

Related

How to change the position and rotation of a Colorscale in folium?

In folium, I have a map with a GeoJson overlay. These overlays are colored differently using a branca colorscale, which is also visible on the map:
colorscale in wrong position
However, I'd like to have the colorscale be in a different position (at fixed coordinates) and rotated by 90 degrees:
colorscale in right position and rotation
Is this possible in folium?
Or would it be more viable to export the colorbar as a png image and add it as an ImageLayer?
I searched the folium and braca documentation, but found no hints.
It seems that changing the position/rotation of the colorscale is not possible in folium (see here). Leaflet, which folium is based on, provides a method for changing the colorscale's position in a rather coarse-grained way (Control.setPosition, which only accepts arguments like "topright"), however this interface is not exposed in folium. Changing the rotation is not supported at all.
I've found a workaround which I'll post below for the sake of completeness. If anyone else faces the same problem, the code will most likely have to be edited. However, it may serve as a starting point. Running the code requires docker to be installed.
def add_colorscale(colorscale, map, x, y):
# Make a blank figure and add the colorscale to it
fig = folium.Map(tiles=None)
folium.TileLayer(opacity=0).add_to(fig)
colorscale.add_to(fig)
# Save figure as HTML
fig.save("temp/temp.html")
# Make background transparent
os.system("sed -i 's/top: 0.0%;/top: 0.0%; background: transparent/' temp/temp.html")
# Convert the HTML to SVG
os.system('docker run -v "$(pwd)/temp:/temp" fathyb/html2svg --wait 5 --format svg "file:///temp/temp.html" > temp/temp.svg')
# Make the background transparent again
os.system("sed -i 's/<rect fill=\"white\"/<rect fill=\"rgba(0,0,0,0)\"/' temp/temp.svg")
# Rotate the SVG (please adjust the numbers by which the graphic is translated depending on your use case)
os.system("sed -i 's/<use href=\"#group_0\"/<use href=\"#group_0\" transform=\"rotate(-90) translate(-2400 200)\"/' temp/temp.svg ")
# Load the rotated SVG and add it at the specified position in the map
with open("temp/temp.svg", "rb") as f:
b64colorscale = base64.b64encode(f.read()).decode("utf-8")
FloatImage('data:image/svg+xml;base64,{}'.format(b64colorscale), bottom=y, left=x).add_to(map)
# cleanup
os.system("rm temp/temp.svg temp/temp.html")
This code than then be called to add a colorscale to an existing map:
add_colorscale(colorscale, m, 0, 0)

How to add image as an icon in a marker with folium

I'm creating a map in python with folium and I've added markers in it, the problem is that I'm trying to change the icon of the marker to a customized png file, can someone help me with this issue? I would appreciate it a lot.
To add an image as an icon in a marker, you can use the folium.Icon class.
Here is an example:
import folium
map = folium.Map(
location=[45.372, -121.6972],
zoom_start=12,
tiles='Stamen Terrain'
)
tooltip = 'Click me!'
folium.Marker(
[45.3288, -121.6625],
popup='<strong>Mt. Hood Meadows</strong>',
tooltip=tooltip
).add_to(map)
folium.Marker(
[45.3311, -121.7113],
popup='<strong>Timberline Lodge</strong>',
tooltip=tooltip,
icon=folium.Icon(icon='cloud')
).add_to(map)
map

Python-pptx compatibility with text boxes in LibreOffice

Just starting to try to use python-pptx, and have fallen at the first. Linux Mint 20.1, python 3.85, LibreOffice 6.4.
This is basically the 'Hello World' from the documentation.
from pptx import Presentation
from pptx.util import Inches, Pt
prs = Presentation()
blank_slide_layout = prs.slide_layouts[6]
slide = prs.slides.add_slide(blank_slide_layout)
left = top = width = height = Inches(1)
txBox = slide.shapes.add_textbox(left, top, width, height)
tf = txBox.text_frame
print('before text ', txBox.left, txBox.top, txBox.width, txBox.height)
tf.text = "This is a long line of text inside a textbox"
print('after text ', txBox.left, txBox.top, txBox.width, txBox.height)
prs.save('test.pptx')
The text is more than a single line for the textbox. Printing out its bounds before and after text insertion shows that as far as python-pptx is concerned, the textbox width hasn't changed.
When the resulting presentation is viewed in LibreOffice, instead of wrapping the text within its boundaries, the textbox has expanded symmetrically around the mid point, pushing the start of the text off the lefthand edge of the page.
I was hoping the LibreOffice was only incompatible with powerpoint for rare edge cases, but text in text boxes is the meat and bread of presentations.
When I upload it to google slides, the text wraps within the left and right text box boundaries, but spills out of the bottom edge. The textbox shows up as 1" x 1" in the right place.
If I use onedrive.live.com, the text is left justified in the box and spills out of the righthand side without wrapping, and the textbox is shown as being 1" x 1" in the right place.
If I use onlinedocumentviewer.com, the display is the same as onedrive, though I can't get to see the text box.
Unfortunately I can't test the behaviour on a native powerpoint installation.
Maybe there's an autosize or fixed flag, which left unset leaves each viewer defaulting it in its own idiosyncratic way? How do we control text boxes / frames when targetting LibreOffice?
I possibly have a workaround to break up my text into single lines and use one per text box, but I'd rather understand the whether it can be done the proper way.
After some floundering around in the docs, I stumbled across the text_frame .word_wrap property. Setting to True gets the text to wrap in LibreOffice. While I was there, setting text_frame.auto_size = MSO_AUTO_SIZE.TEXT_TO_FIT_SHAPE reduces font size to get it all to fit in the box.
Is there a list of properties like .word_wrap in one place anywhere?

Is there a way to plot many markers in Folium?

I am trying to use Folium for geographical information reading from a pandas dataframe.
The code I have is this one:
import folium
from folium import plugins
import pandas as pd
...operations on dataframe df...
map_1 = folium.Map(location=[51.5073219, -0.1276474],
zoom_start=12,
tiles='Stamen Terrain')
markerCluster = folium.plugins.MarkerCluster().add_to(map_1)
lat=0.
lon=0.
for index,row in df.iterrows():
lat=row['lat]
lon=row['lon']
folium.Marker([lat, lon], popup=row['name']).add_to(markerCluster)
map_1
df is a dataframe with longitude, latitude and name information. Longitude and latitude are float.
I am using jupyter notebook and the map does not appear. Just a white empty box.
I have also tried to save the map:
map_1.save(outfile='map_1.html')
but also opening the file doesn't work (using Chrome, Firefox or Explorer).
I have tried to reduce the number of markers displayed and below 300 markers the code works and the map is correctly displayed. If there are more than 300 Markers the map returns to be be blank.
The size of the file is below 5 MB and should be processed correctly by Chrome.
Is there a way around it (I have more than 2000 entries in the dataframe and I would like to plot them all)? Or to set the maximum number of markers shown in folium?
Thanks
This might be too late but I stumbled upon the same problem and found a solution that worked for me without having to remove the popups so I figured if anybody has the same issue they can try it out. Try replacing popup=row['name'] with popup=folium.PopUp(row['name'], parse_html=True) and see whether it works. You can read more about it here https://github.com/python-visualization/folium/issues/726

Openpyxl adjust image between two cells

I'm trying to adjust image between two cells using openpyxl. My problem is that
worksheet.add_image(image, position)
method only accepts top-left position of image.
Is there any way how to scale my image between two (top-left, bottom-right) cells?
I tried to compute dimensions using
height = sum([worksheet.row_dimensions[start_row+i].height for i in range(img_cols)])
and then setting it as
from openpyxl.drawing.image import Image
Image(image_filename, size=[width,height])
but that doesn't work either
You will be able to do this in openpyxl 2.5 but will have create and manage your own anchor.

Categories

Resources