Difficulty in making Python dictionary (xpath/CSS selector) - css

I have a below code and would like to make a dictionary as {"100":"ABC"} and {"200":"DEF"}
<ul class="net">
<li id="100">
<a>ABC</a>
</li>
<li id="200">
<a>DEF</a>
</li>
</ul>
I tried with below code but I don't know how to pass a variable in xpath attribute.
list_of_id = response.css('ul.net>li::attr(id)').extract()
for i in list_of_id:
j = dict()
j[i] = response.xpath('//ul/li[#id="i"]/a/text()').extract()
Please help.

Change this:
j[i] = response.xpath('//ul/li[#id="i"]/a/text()').extract()
to this:
j[i] = response.xpath("//ul/li[#id="+i+"]/a/text()").extract()

You can use selectors to serve the purpose. Give it a shot:
html='''
<ul class="net">
<li id="100">
<a>ABC</a>
</li>
<li id="200">
<a>DEF</a>
</li>
</ul>
'''
from lxml.html import fromstring
tree = fromstring(html)
for item in tree.cssselect(".net li"):
num = item.attrib['id']
name = item.cssselect("a")[0].text
item={num:name}
print(item)
Results:
{'100': 'ABC'}
{'200': 'DEF'}

Related

Remove the list for categories children

I list my categories with wp_list_categories().
When there are subcategories, the result/html gives by default :
<ul>
<li>category parent</li>
<ul class="children">
<li>category children</li>
</ul>
</ul>
I would like to remove the ul list for the catégories children, to have a simple list of categories:
<ul>
<li>category parent</li>
<li>category children</li>
</ul>
Do you know how can it be done?
The easiest way is using jQuery
var innerUl = $(".children").html();
$(".children").remove();
$("ul").append(innerUl);
Or you can use .unwrap(), it would be a better solution.

access parent sub array key handlbars

I have the following array / sub array structure
"filters": {
"types": {
"34": "Ford",
"22": "Jeep",
"18": "Porsche",
},
"locations": [
"Earth",
"Mars",
"Moon",
]
}
and the following handlebars template
{{#if filters}}
{{#each filters}}
<div class="cars">
<ul class="cars__list">
<li class="cars-{{#key}}__title">Filter by {{#key}}:</li>
<li class="cars-{{#key}}__filters">
<ul>
<li class="cars-{{#key}}">View All</li>
{{#each this}}
<li class="cars-{{*want to access filters[key]*}} color={{#key}}">{{this}}</li>
{{/each}}
</li>
</li>
</ul>
</div>
{{/each}}
{{/if}}
I'm having trouble accessing the filters[types] and filters[locations] within the each this loop.
In my CSS I'm using a classes called .cars-type and .cars-location. I want to be able to style each list separately and unfortunately target each li with a class. I want to apply these styles within the each this loop.
I can do it within the filters loop by using {{#key}} but not in the each this loop
I've tried
<li class="cars-{{../filters}}">{{this}}</li>
but this just returns the car type like ford - I want the key ie. '34' in this case
<li class="cars-{{lookup ../filters #index}}">{{this}}</li>
using handlebars helper lookup but again no luck
<li class="cars-{{../this}}">{{this}}</li>
and the above which gives me [object Object]
I've checked out handlebars - is it possible to access parent context in a partial?, handlebars.js: relative paths in partials not working and Lookup helper in handlebars but no luck with any of the solutions
EDIT Here's the HTML output that I want to produce
<div class="cars">
<ul class="cars__list">
<li class="cars-types__title">Filter by types:</li>
<li class="cars-types__filters">
<ul>
<li class="cars-types">View All</li>
<li class="cars-types color-34">Ford</li>
<li class="cars-types color-22">Jeep</li>
<li class="cars-types color-18">Porsche</li>
</ul>
</li>
</ul>
<ul class="cars__list">
<li class="cars-locations__title">Filter by locations:</li>
<li class="cars-locations__filters">
<ul>
<li class="cars-locations">View All</li>
<li class="cars-locations color-0">Earth</li>
<li class="cars-locations color-1">Mars</li>
<li class="cars-locations color-2">Moon</li>
</ul>
</li>
</ul>
</div>
You should reconsider your HTML because a ul cannot be a direct child of another ul. See https://developer.mozilla.org/en/docs/Web/HTML/Element/ul#Usage_context
With that said, we can solve your problem. The Handlebars docs have our answer:
Nested each blocks may access the interation variables via depth based
paths. To access the parent index, for example, {{#../index}} can be
used.
Therefore, your problematic line should look like the following:
<li class="cars-{{#../key}} color-{{#key}}">{{this}}</li>

Handlebars - partial template not getting the context

I have pre compiled all my templates into one js file and loaded the js in the page.
I have 3 templates
Template 1 - a.handlebars
<ul>
{{#each this}}
<li class="a">
{{#key}}
{{> b}}
</li>
{{/each}}
</ul>
Template 2 - b.handlebars
<ul>
{{#each this}}
<li class="b">
{{#key}}
{{> c}}
</li>
{{/each}}
</ul>
Template 3 -c.handlebars
<ul>
{{#each this}}
<li class="c">
{{ this }}
</li>
{{/each}}
</ul>
I have the following JS which load data to the template
var menu= {
a1:{
b1:[c1, c2, c3],
b2: [c4, c5, c6]
},
a2:{
b3:[c7, c8],
b4: [c9]
}
};
Handlebars.templates.a(menu);
The first template (a.handlebars) loads a1 and a2. But when the first template loads the partials b.handlebars it fails. I tried to put a log statement to see what is that I am getting for "this". I got 4 spaces and it was of type string.
I do not understand why b.handlebar is getting a string when I thought I will be getting (example for a1)
{
b1:[c1, c2, c3],
b2: [c4, c5, c6]
}
Previously,I did not pre compile the handlebars. I had all the handlebars in my html and compiled it using Handlebars.compile it worked fine. I do not know why the code is failing after per compiling.
Any help is highly appreciated.
I am trying to get his as my final result:
<ul>
<li class="a">a1
<ul>
<li class="b">b1
<ul>
<li class="c">c1</li>
<li class="c">c2</li>
<li class="c">c3</li>
</ul>
</li>
<li class="b">b2
<ul>
<li class="c">c4</li>
<li class="c">c5</li>
<li class="c">c6</li>
</ul>
</li>
<li class="b">b3</li>
</ul>
</li>
<li class="a">a2
<ul>
<li class="b">b3
<ul>
<li class="c">c7</li>
<li class="c">c8</li>
</ul>
</li>
<li class="b">b4
<ul>
<li class="c">c9</li>
</ul>
</li>
</ul>
</li>
</ul>
But I am getting this. Since the b.handlebars is not getting the object data.
<ul>
<li class="a">a1
<ul>
</ul>
</li>
<li class="a">a2
<ul>
</ul>
</li>
</ul>
My apologies for the very lengthy post. I have been trying to find a solution for this for a while now. Again, any help to resolve this issue would be great.
Thanks.
The issue was caused due to compiling the handlebar/template files using grunt.
Command: "grunt handlebars"
Once I compiled the files using handlers itself, the code started working.
Command: handlebars template_files -f output_file
If anyone knows how to make the compiled code work using grunt command I would definitely would like to know. Thanks.

How to use Sinatra to highlight navigation menu when on current page?

I am having trouble calling the css to indicate the current page that will allow a color tab to show when a page is clicked. I know the "current-menu-item" class works because when you do:
<li id="menu-item" class="current-menu-item">
About Us
</li>
You are able to see the properties associated with the task. I don't know what else to try and I am looking for insight on what I might be doing wrong.
This is my main.rb file
require 'bundler/setup'
require 'rubygems'
require 'sinatra'
require 'sinatra/activerecord'
require 'sinatra/flash'
require 'sinatra/reloader' if development?
require './config/environments'
require 'pathname'
require 'uri'
require './routes/init'
require './helpers/init'
require './models/init'
get '/' do
erb :"index.html"
end
get '/about-us' do
erb :"about_us.html"
end
get '/services' do
erb :"services.html"
end
get '/sign-up' do
erb :"sign_up.html"
end
init.rb
require_relative 'user.rb'
helper1.rb
helpers do
def active_page?(path='')
request.path_info =='/'+path ? 'active_page': nil
end
end
layout.erb
<div class="menu-main-menu-container">
<ul id="menu-main-menu" class="menu">
<li id="menu-item" class="menu-item">
Home
</li>
<li id="menu-item" class="menu-item">
Services
</li>
<li id="menu-item" class="<%= current-menu-item if '/about-us' == active_page? %>" >
About Us
</li>
<li id="menu-item" class="menu-item ">Sign Up</li>
</ul>
</div>
two things, first:
you want to display the string, "current-menu-item" and not the object current-menu-item if the conditional is true, so you want to move the quotes
change:
<li id="menu-item" class="<%= current-menu-item if '/about-us' == active_page? %>" >
to:
<li id="menu-item" class=<%= "current-menu-item" if '/about-us' == active_page? %>>
second:
the conditional and helper method are not used correctly
the helper would be used best if it returned true or false
change the helper method to the following:
def active_page?(path='')
request.path_info == '/' + path
end
then back to the layout:
<li id="menu-item" class=<%= "current-menu-item" if active_page?("about-us") %>>
logically, this will accomplish what you are trying to do.

asp.net literal.text using multiple lines of html code

I am trying to initialize a literal with a big sequence of html code.
for example this is working just fine:
Literal1.Text.= "<ul><li>home<li/><ul/>";//just fine
But:
When i have more than one lines it doesn't work:
Literal1.Text.= "<ul><li>home
</li></ul>";//error
I know a simple solution which is this:
Literal1.Text.= "<ul><li>home"+
"</li></ul>";//nice but time wasting for many lines
But:
When i have 100 lines for example i don't want to spend my time concatenating the strings.Is there any more practical solution?
Specifically my html code is the folowing:
Literal1.Text.= "<ul id="nav">
<li class="current">Home</li>
<li>People
<ul>
<li>Customers
<ul>
<li>View</li>
<li>Edit</li>
<li>Print</li>
<li>Delete</li>
</ul>
</li>
<li>Employees
<ul>
<li>View</li>
<li>Edit</li>
<li>Print</li>
<li>Delete</li>
</ul>
</li>
</ul>
</li>
<li>Quotations
<ul>
<li>Create</li>
<li>Edit</li>
<li>View</li>
</ul>
</li>
<li>Invoices
<ul>
<li>Create</li>
<li>Edit</li>
<li>View</li>
</ul>
</li>
<li>Receipts
<ul>
<li>Create</li>
<li>Edit</li>
<li>View</li>
</ul>
</li>
<li>Statements
<ul>
<li>Create</li>
</ul>
</li>
<li>About Us</li>
<li class="style1">Contact Us</li>
</ul>";
For a small number of lines of code, I generally use StringBuilder. That way there are no performance issues with concatenating large immutable strings.
StringBuilder sb = new StringBuilder();
sb.Add("<ul><li>home");
sb.Add("</li></ul>");
For a large number of lines, don't put them in your code at all. Read them in using System.IO.File, so that you can edit your HTML without worrying about doubling your quotes. If you're using the code a lot, read it in once using a static class so that you're not causing a lot of extra IO usage. Something like this (untested):
public static class HTMLStringClass
{
private static string html;
public static string GetHtmlString()
{
if (string.IsNullOrEmpty(html))
html = File.ReadAllText("path/to/file");
return html;
}
}
And then call it by doing this:
Literal1.Text = HtmlStringClass.GetHtmlString();
Use a verbatim string literal (one that starts with #):
Literal1.Text = #"<ul><li>home
</li></ul>";

Resources