I created a Map using python folium in jupyter lab. On the Map I display some geoJson-Files as shapes.
What Works so far:
The Shapes from the GeoJson file are displayed nicely on the map. I can change the color of the shapes based on a self generated style_function which checks feature['properties']['id'] to adjust the style type accordingly.
I'm also able to get a GeoJsonPopup on_click to a shape. The Popup shows id and the content of the id property of that shape.
geo_popup = folium.GeoJsonPopup(fields=['id'])
my_json = folium.GeoJson(file.path, style_function=style_function, popup=geo_popup)
my_json.add_to(map)
What I want:
I want to display in the popup some content based on the id. Very basic Example: if id = 1 i want to display 'This is the region Alpha' or if id = 2 -> 'This area is beautiful'.
Alternatively, if that is not possible, I would like to present a Link in the Popup where i can Access a Page with a parameter to show dedicated content for that id.
What I tried
I tried to derive a class from folium.GeoJsonPopup and somehow write content to the render function. But, however, I don't really get how it works and therefor all I did wasn't successful. Probably I took somewhere the wrong path and the solution is pretty easy.
Thanks for advice!
I followed the linked sample in the comment to the question. Therefore I had to add the needed dict entries to the the features properties.
Therefore I can link to this question. I used the .update from the solutions last comment to add the values.
Related
So I'm trying to build a tool to transfer tickets that I sell. A sale comes into my POS, I do an API call for the section, row, and seat numbers ordered (as well as other information obviously). Using the section, row, and seat number, I want to plug those values into a contains (text) statement to in order to find and select the right tickets on the host site.
Here is a sample of how the tickets are laid out:
And here is a screenshot (sorry if this is inconvenient) of the DOM related to one of the rows above:
Given this, how should I structure my contains(text) statement so that it is able to find and select the correct seats? I am very new/inexperienced with automation. I messed around with it a few months ago with some success and have managed to get a tool that gets me right up to selecting the seats but the "div" path confuses me when it comes to searching for text that is tied to other text.
I tried the following structure:
for i in range(int(lowseat), int(highseat)):
web.find_element_by_xpath('//*[contains (text(), "'+section+'")]/following-sibling::[contains text(), "'+row+'")]/following-sibling::[contains text(), "'+str(i)+'")]').click()
to no avail. Can someone help me explain how to structure these statements correctly so that it searches for section, row, and seat number correctly?
Thanks!
Also, if needed, here is a screenshot with more context of the button (in cases its needed). Button is highlighted in sky blue:
you can't use text() for that because it's in nested elements. You probably want to map all these into dicts and select with filter.
Update
Here's an idea for a lazy way to do this (untested):
button = driver.execute_script('''
return [...document.querySelectorAll('button')].find(b => {
return b.innerText.match(/Section 107\b.*Row P.*Seat 10\b/)
})
''')
Thanks for you help & time, here is my code, I am accessing a specific cell of a table and my goal is to modify the text context of this specific cell - by that I mean to overwrite the existing text in that cell with a new string value. How do I do that?
def main():
credentials = get_creds()
service = build("docs", "v1", credentials=credentials).documents()
properties_req = service.get(documentId=REQ_DOCUMENT_ID).execute()
doc_content_req = properties_req.get('body').get('content')
properties_des = service.get(documentId=DES_DOCUMENT_ID).execute()
doc_content_des = properties_des.get('body').get('content')
reqs = find_requirements(doc_content_req)
for (req, row) in zip(reqs, req_table.get('tableRows')):
loc = search_structural_elements(doc_content_des, req)
cell = get_cell(row, design_col)
print(f"Requirement {req} is located in section {loc} of the design doc.")
print(cell) # Need to modify the text content of this specific cell
You can easily modify the contents of a particular cell by using the Documents.batchUpdate() tool. First and foremost I strongly recommend you to familiarize yourself with the Table structure inside a Doc. There you can see how the table is first declared, then divided in rows and later formatted with some styles. After you write the desired change in the desired cell (from the desired row), then you can use Documents.batchUpdate() and reach your goal.
As a tip, I want to add that you can run a small trick to easily find the desired cell. This operation can help you manage your first cell edit easily. First you will need to open the Doc in a browser, then you have to write a recognizable string (like 123ABC) in the desired cell. After that you could use Documents.get() to receive the Document object. You can search for the 123ABC string without difficulty in that object, change it to the desired value, and use that object as a template for the batch update. Please ask me any additional questions about this answer.
I currently want to scrape some data from an amazon page and I'm kind of stuck.
For example, lets take this page.
https://www.amazon.com/NIKE-Hyperfre3sh-Athletic-Sneakers-Shoes/dp/B01KWIUHAM/ref=sr_1_1_sspa?ie=UTF8&qid=1546731934&sr=8-1-spons&keywords=nike+shoes&psc=1
I wanted to scrape every variant of shoe size and color. That data can be found opening the source code and searching for 'variationValues'.
There we can see sort of a dictionary containing all the sizes and colors and, below that, in 'asinToDimentionIndexMap', every product code with numbers indicating the variant from the variationValues 'dictionary'.
For example, in asinToDimentionIndexMap we can see
"B01KWIUH5M":[0,0]
Which means that the product code B01KWIUH5M is associated with the size '8M US' (position 0 in variationValues size_name section) and the color 'Teal' (same idea as before)
I want to scrape both the variationValues and the asinToDimentionIndexMap, so i can associate the IndexMap numbers to the variationValues one.
Another person in the site (thanks for the help btw) suggested doing it this way.
script = response.xpath('//script/text()').extract_frist()
import re
# capture everything between {}
data = re.findall(script, '(\{.+?\}_')
import json
d = json.loads(data[0])
d['products'][0]
I can sort of understand the first part. We get everything that's a 'script' as a string and then get everything between {}. The issue is what happens after that. My knowledge of json is not that great and reading some stuff about it didn't help that much.
Is it there a way to get, from that data, 2 dictionaries or lists with the variationValues and asinToDimentionIndexMap? (maybe using some regular expressions in the middle to get some data out of a big string). Or explain a little bit what happens with the json part.
Thanks for the help!
EDIT: Added photo of variationValues and asinToDimensionIndexMap
I think you are close Manuel!
The following code will turn your scraped source into easy-to-select boxes:
import json
d = json.loads(data[0])
JSON is a universal format for storing object information. In other words, it's designed to interpret string data into object data, regardless of the platform you are working with.
https://www.w3schools.com/js/js_json_intro.asp
I'm assuming where you may be finding things a challenge is if there are any errors when accessing a particular "box" inside you json object.
Your code format looks correct, but your access within "each box" may look different.
Eg. If your 'asinToDimentionIndexMap' object is nested within a smaller box in the larger 'products' object, then you might access it like this (after running the code above):
d['products'][0]['asinToDimentionIndexMap']
I've hacked and slash a little bit so you can better understand the structure of your particular json file. Take a look at the link below. On the right-hand side, you will see "which boxes are within one another" - which is precisely what you need to know for accessing what you need.
JSON Object Viewer
For example, the following would yield "companyCompliancePolicies_feature_div":
import json
d = json.loads(data[0])
d['updateDivLists']['full'][0]['divToUpdate']
The person helping you before outlined a general case for you, but you'll need to go in an look at structure this way to truly find what you're looking for.
variationValues = re.findall(r'variationValues\" : ({.*?})', ' '.join(script))[0]
asinVariationValues = re.findall(r'asinVariationValues\" : ({.*?}})', ' '.join(script))[0]
dimensionValuesData = re.findall(r'dimensionValuesData\" : (\[.*\])', ' '.join(script))[0]
asinToDimensionIndexMap = re.findall(r'asinToDimensionIndexMap\" : ({.*})', ' '.join(script))[0]
dimensionValuesDisplayData = re.findall(r'dimensionValuesDisplayData\" : ({.*})', ' '.join(script))[0]
Now you can easily convert them to json as use them combine as you wish.
I apologize, I have been looking for a solution but can't find enough documentation to figure it out. I am trying to import a default slide layout required for school, it has a special background and a Title Block and a Subtitle Block. I assumed when I import this, python-pptx would just automatically create placeholders 0 and 1 for those two text blocks but when I try and edit the placeholders, I get an attribute error:
AttributeError: 'Presentation' object has no attribute 'placeholders'
My code is as follows:
from pptx import Presentation
prs = Presentation('SeniorDesignTitleSlide.pptx')
Presentation_Title = prs.placeholders[0]
Presentation_Subtitle = prs.placeholders[1]
Presentation_Title.text = 'This Is a Test'
Presentation_Subtitle.text = 'Is This Working?'
prs.save('SlideLayoutImportTest.pptx')
Edit[0]: I do realize I am just opening that particular presentation, but how do I access and edit the single slide that’s in it ?
Edit[1]: I’ve found a few posts from 2015 about python-pptx expanding on this feature, but there’s no further information that it actually occurred.
How does python-pptx assign placeholders for imported slide layouts? Or does it even do this? Does it need to be a .potx file?
Thank you in advance.
Placeholders belong to a slide object, not a presentation object. So the first thing is to get ahold of a slide.
A slide is created from a slide layout, which it essentially clones to get some starting shapes, including placeholders in many cases.
So the first step is to figure out which slide layout you want. The easiest way to do this is to open the "starting" presentation (sometimes called a "template" presentation) and inspect it's slide master and layouts using the View > Master > Slide Master... menu option.
Find the one you want, count down to it from the first layout, starting at 0, and that gives you the index of that slide layout.
Then your code looks something like this:
from pptx import Presentation
prs = Presentation('SeniorDesignTitleSlide.pptx')
slide_layout = prs.slide_layouts[0] # assuming you want the first one
slide = prs.slides.add_slide(slide_layout)
Presentation_Title = slide.placeholders[0]
Presentation_Subtitle = slide.placeholders[1]
Presentation_Title.text = 'This Is a Test'
Presentation_Subtitle.text = 'Is This Working?'
prs.save('SlideLayoutImportTest.pptx')
The placeholders collection behaves like a dict as far as indexed access goes, so the 0 and 1 used as indices above are unlikely to match exactly in your case (although the 0 will probably work; the title is always 0).
This page of the documentation explains how to discover what indices your template has available: http://python-pptx.readthedocs.io/en/latest/user/placeholders-using.html
The page before that one has more on placeholder concepts:
http://python-pptx.readthedocs.io/en/latest/user/placeholders-understanding.html
I am trying to use an existing template design of powerpoint using python-pptx library. My problem is that I have two or more different templates ready and when I viewed their slide master, the "title and content layout" of each template are not on the same order. So, the index that I will use will be 1 if I used the first templates and 2 for the second templates.
Using the python-pptx library:
Sample Python Code 1 for fist templates
bullet_slide_layout = self.prs.slide_layouts[1]
Sample Python Code 2 for second templates
bullet_slide_layout = self.prs.slide_layouts[2]
Both of them works, but I do not want to change the indices every now and then whenever a new template design is added.
Please help. Also, If I am not clear with the problem I presented, please tell me. Thank you
If you want to retrieve a slide layout by something other than its position in the layout sequence, you will have to write something of your own.
There are a few approaches:
Use the slide layout name
Use the slide layout id
Characterize the slide by the number and type of placeholders it contains and perhaps their size and position.
So as an example, something simple would be:
def get_layout_by_name(prs, layout_name):
for layout in prs.slide_layouts:
if layout.name == layout_name:
return layout
return None