Monday, January 2, 2012

A Waitress & A Flask

Flask is a microframework for Python based on Werkzeug, Jinja2, and here is a simple Flask Hello World code:

from flask import Flask
app = Flask(__name__)

@app.route("/")
def hello():
    return "Hello World!"

if __name__ == "__main__":
    app.run()

Waitress on the other hand is a pure-Python WSGI server, and here's normal usage of the server:

from waitress import serve
serve(wsgiapp, host='0.0.0.0', port=8080)

So, here is how I wrote code to run Flask from within a Waitress WSGI Server:

from flask import Flask
from waitress import serve

app = Flask(__name__)

@app.route("/")
def hello():
 return "Hello World!"

if __name__ == "__main__":
 #app.run()
 serve(app)

Documentations:
http://flask.pocoo.org/docs/
https://github.com/Pylons/waitress/blob/master/docs/index.rst

Wednesday, December 28, 2011

Quick jQuery Intro.

For the second time in two days, one of my friends asks me about jQuery. So, here is a quick introduction to it. Let's say we have the following HTML that contains a list of four items
<html>
<head>
   <title>jQuery demo</title>
</head>
<body>
   <ul>
     <li class="news_line">Mr. One</li>
     <li class="news_line">Mr. Two</li>
     <li class="news_line">Mr. Three</li>
     <li class="news_line">Mr. Four</li>
   <ul> 
</body>
</html> 
Now we want to make it more interactive, so when the mouse hovers on any of the below items it turns red, and when it goes away it turns white again. Here comes the beauty of jQuery, you can give them all a CSS class "news_line", hence you write a function once and it is applicable to them all. So, first of all, you have to include the following script.
<script 
 src="http://ajax.googleapis.com/ajax/libs/jquery/1.5/jquery.min.js"
></script>
You put all your jQuery script between the following, this makes sure it's not executed until the page is loaded successfully
<script>
    $(document).ready(function(){
       ... YOUR jQUERY SCRIPT HERE ...
    });
</script>
We now are going to use the following function, which simply mean attach an event catcher to the items with class "news_line", and make the event trigger is 'mouseover', i.e. whenever the mouse hovers over it. We also have other events such as 'mouseout', etc.
$(".news_line").live('mouseover',function(){
   ... DO SOME STUFF HERE ...
});
One more thing to notice is "$(this)", the above command searches for all items with class "news_line", now when we use "$(this)", we are referring the the item we are dealing with now. And starting from that point we use another functions "css()" to change the object's background colour. You may in some other cases use stuff like "$(this).children().css()" to change the colour of all of the children of the matched items, or "$(this).parent().parent().hide();" to hide the parent of the parent of the item you are referring to, etc. So, finally here is the code we are talking about here.
<html>
<head>
   <title>jQuery demo</title>
   <script src="http://ajax.googleapis.com/ajax/libs/jquery/1.5/jquery.min.js"></script>
   <script>
     $(document).ready(function(){
        $(".news_line").live('mouseover',function(){
           $(this).css("background-color","#ff0000");
        });
        $(".news_line").live('mouseout',function(){
           $(this).css("background-color","#ffffff");
        });
     });
   </script>
</head>
<body>
   <ul>
     <li class="news_line">Mr. One</li>
     <li class="news_line">Mr. Two</li>
     <li class="news_line">Mr. Three</li>
     <li class="news_line">Mr. Four</li>
   <ul> 
</body>
</html> 

Finally, here is a list of all jQuery functions, events, etc.
http://visualjquery.com/

Thursday, September 29, 2011

Delicious Redesign

As you know, Yahoo sold their social bookmarking service, Delicious, to AVOS, a while ago. On September 26, AVOS redesigned Delicious. It's sad how Facebook grabs the whole internet's attention with every redesign they make, while Delicious redesign was ignored like that. However since I am a Delicious fan myself, let me write my two cents here about their new design, which by the way is not only an interface redesign, but many functionalities have been changed too.


  • Avatar: Now you can add your own Avatar (profile picture) to your profile there. Silly!? Not really, I guess it's meant to make the service more social. Although Delicious was one of the pioneers in the Social Web movement, they have been a bit anti-social compared to newer services like twitter, espcially when Yahoo stopped any further development in the site for ages.
  • Stacks: Well, before talking about that feature, let me tell first you how I used to use Delicious earlier. Other than saving my bookmarks, Delicious was like my way to discover new startups, python libraries, news, etc. All that was done via two tabs that were normally ignored by many users, the Network tab and the Subscription one. The Network tab was like the timeline of my friends' bookmarks, or you can say it was the twitter-like face of Delicious. In the Subscriptions, you were to subscribe to some tags related to interesting topics to you, and it lists all the links saved under those tags whether by your friends or by strangers. Now those two tabs aren't there any more, and it seems that the Stacks feature is meant to replace them, it's like a way for people to curate links under a specific subject, give the whole curation a title and description. You know, it's like a less-geeky replacement for the old tags pages, which are still there by the way. Currently, you should curate the links within a stack manually, however I guess they will let us grab them from specific tags later on. Now if you ask me, are Stacks really a good replacement for Network/Subscriptions, well, I am getting older and don't get used to changes easily, but I guess after a while I'll get used to it as a replacement for the Subscriptions tag, however I'll still miss the Network unless it is - or both of them are - still there but hidden somewhere in the new interface and I am not able to find them!!
  • API: It's cool they left the API as it is for backward compatibility, as now I can still use the same bookmark button I have in my browser. Also, in case any developers had applications built using the old API, they should still function now. However I believe there should be additions to reflect the new features. Are the "tag bundles" mentioned in the API docs for example meant to reflect the Stacks? I have no idea.
  • Rough Edges: Some rough edges are there with the new design, but this isn't a big deal as they are minor one, and they made it clear that they are back to beta status and are looking for users feedback now. 
  • Auto-completing Tags: I've noticed that tags whether in the Bookmarklet or on the site itself ain't auto-completed. Is it something they are working on to improve, or is it a new trend! Hope it's the former not the later. The auto-completion of tags isn't just meant to make our life easier by typing less, but more important, it prevents us from creating too many with slightly different spellings in reference to the same thing, startup, startups, start-up, etc. 
Tags: , ,

Thursday, December 16, 2010

Fuck! Yahoo is shutting Delicious down!

People everywhere all over twitter are arguing if Mark Zuckerberg deserves to be named Time Magazine's Person of the Year 2010 or not. But on the other hand, I am sure we all have to agree who really deserves to be the arse of the year. Or may be the arse of the decade if you want.

Yahoo was an internet search company ages before the birth of Google. And now, they sold their soul to Microsoft and Yahoo Search is just an interface for Bing! Yahoo created GeoCities ages before the real blogging hype. Blogs came, then came micro-blogs, and GeoCities stood stand still. And recently GeoCities was discontinued. Yahoo 360 and MyBlogLog were there when Zuckerberg was still using nappies. They were there before Facebook Connect and Google Friend connect, and guess what, the two services are discontinued now. FireEagle is such a brilliant idea, it's the mother of all Gowalla/Foursquare thing, and now it's been more than a year with no significant development there and seems that it will be shut-down soon. Yahoo's Webring and Briefcase are also dead now. And finally my most beloved services that Yahoo ever had are Delicious and Flickr, and today I heard that Delicious is to be shut-down soon, and God only knows when Flickr will be shut-down too.

Every company has its failures, and there is a rule of thumb that roughly 50% of acquisitions fails. Google had its failures too. Google's Wave and Buzz suck a big time. But the difference here is that Yahoo doesn't give its product the chance to succeed or fail, they just shut them down blindly. Only a looser can have a service as successful as Delicious and shut it down, and it requires a huge amount of dumbness to turn successful services like GeoCities, Yahoo Search, and MyBlogLog into failures.

A few months ago Yahoo made - AFAIK - the first acquisition ever of an Arab company in the field of technology. The target of their multi-million acquisition was one of the crappiest Arab companies ever, Maktoob! I've encountered few of their products, such as their blog-hosting service, and it was so 1990's.

Delicious! I really can't believe it! So what the heck is really left out of Yahoo? Their email!? Ok, this one really sucks, and it sure should have been discontinued ages ago instead of Delicious.

In case you haven't noticed yet, I used to be a big Yahoo fan. I used create accounts in almost all of their services, even those obscure ones. Yet now, it's sad to admit that it looks like Yahoo is dying.

Finally, I need your recommendations now for migrating my bookmarks. Meh, should I migrate to those crappy service that I used to hate, such as Diigo and StumbleUpon :(

Update: Apparently they are going to spin Delicious off instead of shutting it down.

Wednesday, February 24, 2010

Safe Position Updates

Brightkite - our nice competitors - have published a very good post about "The Best Practice for Safe Position Updates", or if you prefer their own jargon, then it's "Safe Check-In's".

http://blog.brightkite.com/2010/02/17/practicing-safe-check-in/

Tuesday, November 24, 2009

The Future of Baralbait

Every now and then we see people coming out with cool startup, and we also sometimes see other people shutting their own startups down. And each time I see any of those startups being shut down I keep asking myself, why did they take such decision. Does it really cost them a lot of money, and they don't have any revenue streams to cover their expenses?

But lately, I've been haunted by the idea of shutting Baralbait down. Baralbait doesn't cost me much money at all, so it's not financial reasons that makes me think of shutting it down. Also, I hate when people keep blaming the competition, yes, we have got Four Square, Bright Kite, Google Latitude, Loopt, Yahoo Friend on Fire, Dopplr, and so many other cool services that are more attractive than Baralbait, but it's not the competition that makes me wanna quit. And for sure dumbs are the only ones who prefer to blame the market - i.e. their users.

I've started Baralbait more than a year ago. And so for we have less than 100 users. Most of them, signed up, and gave Baralbait a try for few minutesn and never logged in again. I really, don't know if I am wasting my time trying to solve a problem that doesn't exist. But still I can't say that this is the real reason for me to take such decision, because I still believe that people always face problems when they decide to go out as they have to think of a place to go, something to do, and friends to arrange the outing with. So, I can tweak and morph my service more to be a suitable solution for such problems, as apparently it doesn't help in solving those problems so far.

In fact, I am still not sure why I want to quit, but may be I want to focus on other ideas that can be more useful to people than Baralbait.

PS. I haven't taken a final decision yet. I am still thinking about it, and your feedback will sure help me decide.

Thursday, October 8, 2009

CherryPy Custom Authentication

While working on Baralbait's API - yes, we may have an API someday, and you can sure contact us if you need to know more about it. Anyway, while developing the API, we were planning to use CherryPy Basic Authentication, in order to authenticate the API Calls.

This is how to add CherryPy Basic Authentication to one of your methods/pages:
import cherrypy

users = {"user1": "secret1", "user2": "secret2"}

def clear_text(mypass):
return mypass

class MyServer:

def index(self):
return "This is a public page!"
index.exposed = True

def secret(self)
print "Logged user is:", cherrypy.request.login
return "Awesome, you've just accessed a page that requires HTTP Basic Authentication"
secret.exposed = True
secret._cp_config = {'tools.basic_auth.on': True,
'tools.basic_auth.realm': 'My Secure Server',
'tools.basic_auth.users': users,
'tools.basic_auth.encrypt': clear_text}

if __name__ == '__main__':

cherrypy.quickstart(MyServer(),"/","myserver.conf")

The above code means, that the page called "index", is a public page, while "secret" requires HTTP Basic Authentication, with the credentials mentioned in the "users" dictionary. For more info, Jim Hoskins has an awesome tutorial about using HTTP Basic Authentication in CherryPy, however his site is down now :(

Now, the problem with the above code, is that you can either make a certain page public or secured, but you cannot make it public and private in the same time. Ok, please be patient, let's say that you want authenticated users to see certain content when visiting our secret page, while unauthenticated users should see different content instead of being blocked. For example, we want authenticated users to see their friend's news feed, while unauthenticated users see public news.

So, here comes the beauty of CherryPy Custom tools. You can now, build your own authentication hook, and make it return a null or custom user id when an incorrect or no username or password are given, instead of totally blocking the user.

And here are the modifications needed to the above code:
import cherrypy

from cherrypy.lib import httpauth

users = {"user1": "secret1", "user2": "secret2"}

def clear_text(mypass):
return mypass

def my_basic_auth(realm, users, encrypt=None):
if cherrypy.lib.auth.check_auth(users, encrypt):
print "DEBUG: Authenticated"
return
else:
print "DEBUG: Not Authenticated"
cherrypy.request.login = "Anonymous"
return

class MyServer:

def index(self):
return "This is a public page!"
index.exposed = True

def secret(self)
if cherrypy.request.login == "Anonymous":
return "This is another public page on our useless website."
else:
return "Can you keep a secret, this page is really confidential."
secret.exposed = True
secret._cp_config = {'tools.my_basic_auth.on': True,
'tools.my_basic_auth.realm': 'My Secure Server',
'tools.my_basic_auth.users': users,
'tools.my_basic_auth.encrypt': clear_text}

if __name__ == '__main__':

cherrypy.tools.mybasic_auth = cherrypy.Tool('on_start_resource', my_basic_auth)
cherrypy.quickstart(MyServer(),"/","myserver.conf")

So, we have just created a custom tool, and hooked it in the earliest hook ever, 'on_start_resource', i.e. during the request. We also created our own authentication method, 'my_basic_auth', and attached it to the tool. In our authentication method, which is almost identical to CherryPy's built in HTTP Basic Authentication method, however we do not raise any errors regardless the user is connected or not, we just set 'cherrypy.request.login' to an arbitrary user, that our application can understand later on, such as 'Anonymous'.