2019-05-01_Linux_Format

(singke) #1

52 LXF249 May 2019 http://www.linuxformat.com


TUTORIALS Search Google


Python3 editorfoundintheProgrammingsectionof
theRaspbianmenu.Whentheeditoropens,clickFile>
Newtocreatea newblankdocument.Nowimmediately
savethefileusingFile> Save,callitinspiration.pyand
remembertosaveoften.

EasynowGUI
Westartourprojectbyimportinga seriesoflibraries/
modules.Firstlyweimportthesearch functionfrom
thegooglesearchlibrary,thenweimportthechoice
functionfromtherandomlibrary.Wenextimportthe
webbrowserlibrarytoenableustoopena browser
window.Finallyweimporttheeasyguilibrary,butwe
shortenthelibrarynameto‘eg’,as‘easygui’canbe
trickytotypeatspeed.
fromgooglesearchimportsearch
from random import choice
import webbrowser
import easygui as eg
The first step in this project is to ask the user a
message, and to do this we use easygui’s msgbox
(message box) function. Here we can ask a message
( msg ), give the dialogue box a title and change the text
in the ok_button to ‘Inspire Me!’
eg.msgbox(msg=”Tell me what you would like in a
project and I shall search Google for you. This will
trigger 3 pop up windows”, title=”InPIration”, ok_
button=”Inspire Me!”)
Next we need to create two blank lists. One will
contain the things that we want to search for, the
other will store the suggestions that our Google search
finds. Lists in Python are called arrays in other
languages. They can contain lots of pieces of data, for
example strings, integers, floats and variables, but not
exclusively these. We can call data from a list by using
its index number. A list starts the index from zero and
can go on and on.
things = []
suggestion = []
In the msgbox we instruct the user that we are
going to ask them for three things to search for – these
are the items that they wish to use as inspiration. To do
this we need to use a for loop with a range. In this case
it will create a variable called i and each time the loop
goes round it will iterate (add) one to the variable until it

achissuewewritea newRaspberryPiproject
andeachissuewetrytobeinventiveand
unique.Buttherearetimeswheninspirationis
notwithus,andinthosemomentsweturntosearching
Googlewithkeyphrasesorwords,andoftentheyare
quiteridiculous.These‘mashups’aretheretofirethe
imagination;justasa childgivenconstructionblocks
canmakeanythingtheydreamof,wecanusethese
sillysearchestothinkupnewanduniqueideas.
Inrealitythisprojectisreallyjustthreefor loops
andtwoliststhatstoreanditerateoveruserinputwhile
it searchesGoogle.Thenit opensa randompageinour
webbrowser.

Let’sgetmashing!
BeforewecanwriteanyPythoncodewefirstneed
toinstalltwoextralibrariesofcode.Thesearecalled
packages or modules, and they contain pre-written
code that we can reference in our project. The first
library will install a way in which we can search Google
from Python. The second library is an easy to use
graphical user interface toolkit. In a terminal enter
the following command.
$ sudo pip3 install google easygui
This will take a few moments. Once it’s complete,
close the terminal window. To write the code we need
to use a Python editor – in this project we’ll use the

Our app is a simple
tool to use silly
keywords to fuel
our imagination for
another cool project.

Get inspiration for your


projects with Google


LesPounder
isa freelance
makerwhoworks
withorganisations
suchasthe
RaspberryPi
Foundation.


AnyPi
Raspbian
Code:
https://
github.com/
lesp/LXF249-
InsPIration/
archive/
master.zip

E


EASYGUI


OUR
EXPERT

YOU NEED


Les Pounder shows you how to use Python to search Google


to gain inspiration for your next project.

Free download pdf