Python Course in Delhi | Interact Functionality with GUIs
Welcome everyone to this bonus section on Python course Julys or graphical user interfaces with IPA widgets.
So we're going to show you how to build simple graphical user interfaces with Jupiter.
Moreover, because we're going to do this exclusive live Jupiter, that's why we consider this bonus material of python training in delhi.
There are lots of libraries out there for graphical user interfaces each with their pros and cons.
There's the built-in T.K. enter which doesn't look that great, but it's built into Python.
There are also solutions like pie cutesie which then if you want to use it as a product there's
some licensing issues you need to deal with.
However, there's the open source IPA widgets and widgets styling structure of Jupiter.
So that's why we decided to use it for this kind of bonus material section.
So we're going to explore how to build interactive elements in Techstack live class.
These are things like buttons or sliders that you can have directly in a notebook for someone else to
play around with.
You're going to want to keep the electronic books handy because there's a lot of reference code and
the information there for you.
So let's get started with our very first python course on this topic.
We're going to discuss the interactive functionality.
Let's open up a super notebook.
OK.
So here I am at a Jupiter notebook as a rapid note for the notebooks that go along with this python course.
that are hosted at the depot.
You actually won't be able to see the sliders that we create or any of the interactive elements that.
we're going to be creating.
That's because this is just hosting the code itself.
It's not running it.
So any viewer or get a hub will be able to execute the code here.
They're just showing you the code itself.
So keep that in mind if you come to visit the hub and you're wondering why you don't see sliders here.
You need to download snowpack and run it you know book to see or view the widgets
moreover, interact with them.
So keep that in mind.
Moreover, again this is all under bonus material introduction to DUIs or guides So let's get started with the
Interact function to do that we need to say from IPA widgets import interact, and you can use
tab autocomplete here interactive and also fixed, and then we'll say import I-Spy widgets as widgets
so once we're done with those imports, we're going to create probably the most basic interactive function.
So the most basic level interact does it auto-generates a user interface control for some function
argument and then it calls the function with those arguments that you can allow yourself to manipulate
interactively.
So to use a track we first need to define a function that we want to play around with or explore.
So we're going to create the most basic function possible which is just well it's not the most basic.
function possible but it's a basic one.
You take an x, and we're turnback x.
So a super simple function here and then we're going to call interact, and you pass and the function.
itself and then you set some default value for all the arguments of the function, in this case, there's
one argument and then we run this we should see a slider here.
So then know how as I'm sliding I have this x value, and this is my input X I have highlighted here and
this is my output x.
So if I scroll around or slide around, I get to see the actual results.
So let's explore this concept further.
Let's imagine I wanted to return the square of x.
If I rerun my function and rerun interact here.
Now I'm returning to square at the bottom.
So I can see here as my slider goes from negative 7 to 25 I'm just returning the square there.
So I can see here how I can vary the input and then see some corresponding output.
Let's go back to that simple function where we return x.
So we saw that we get a slider.
We were working with integers.
Let's see what happens when we passed booleans.
So of booleans it auto creates a checkbox which makes sense because Blansett is true or false.
So here now we automatically created the checkbox market check for real on the market for false and then
if we pass in a string such as Hello, then I get back this auto text box.
So right now this function is just accepting X and returning X and interact smart enough to know.
what kind of feature it should show you based on the data type is being passed.
So for integers or numbers, it passes in a slider.
We just saw four booleans that pass in a checkbox and then for strings it passes in a little text
box here.
So I can type whatever I want.
You notice that this function is just returning x.
So lots of cool functionality here.
Now we're going to do instead of passing in this function into interact we can do all this
with a decorator.
So let's see how we can do that.
I'm going to click exit here to kind of end this I'll come back to the function.
Let's create a new function called G and G takes an X and Y and then all it does.
It just returns a tuple of X and Y a straightforward function.
Now what we could have done is pass this function into interact again and then provided something for
x and something right.
However, we can do all of this with the decorator.
Moreover, if you're unfamiliar with decorator's make sure to go back and review that decorator's python course.
However, there's a decorator called interact.
Moreover, here we can sand the default values that we want to start with.
So I can say something like X is equal to, and Y is equal to a floating point let's say one point
zero.
Moreover, now when I run this, I get back these options so I can say true false.
That is my output here.
Moreover, then I have this slider.
Now let's imagine we wanted to keep one of these values fixed.
We don't want everything to be manipulated by the user.
Well, we could do pass in this parameter into the fixed function.
So we would do here is to say why is equal to fixed 1.0.
Make sure my princes balance up and then when you run this you no longer have the option to pass and
why it's been fixed at one point zero.
Moreover, this also works inside the Interact function.
So if we rerun phunk here and I say x is equal to hello.
Moreover, I say fixed hello.
Then when I run this, I don't get the option of that text box anymore.
It just fixes this parameter always to be whatever that the fault was.
So that's how you can use the fixed function.
Moreover, so now we have the Interact function as well as the Interact decorator.
Now let's talk about which abbreviations.
Recall that when we said interact pass in our function and then set x is equal to let's say one we get
back this value of a slider and I can go to some minimum and see a maximum.
However, notice what's happening here.
I can only go to a minimum of the negative one and a maximum of three.
Moreover, if I were to expand this to 10, suddenly, I can go to a max of 30 and a moon minimum a negative 10.
So what happens is, and your maximum is defined by x times 3.
So you may be wondering well how do I change my minimum maximum and even my step size or my
starting value.
Well, you can do instead of just saying equal to 10 and have it automatically create this integer.
slider for you-you can call widgets WIPs I.A. slider and then you can find pasan if you do shift tab here
moreover, expand this you'll see the arguments pasan you can pass in a minimum, maximum step and value.
So let's do that as well.
Remember this is an integer slider for step size needs to be an integer.
There's also a float slider in case you want that.
However, now I can say my minimum goes to negative 100.
My maximum goes to positive 100.
We can create a step size of one.
Moreover, then our starting value of 0.
So now I have all these options with the integer slider, and if we run this notice, I'm starting at zero.
I have a step size of one, and I go from negative 100 to positive 100.
So these are the widgets that you can specifically call if you want more options and the functionality.
of the widgets instead of just the ones that are auto-created for you now because sliders are so commonly
used with widgets, there are ways to abbreviate all this functionality here.
Moreover, the way you do that is by calling interact passing your function and say x is equal to and it's
very similar to a range we do you have a tuple here where you have a min-max and a step size.
So I could say go from negative 100 to 100 step size of one.
Moreover, then when I run this notice back, I get back the same thing from negative 100 to 100.
Alternatively, I can go from negative 9:50 rerun this and here is negative tend to 10 step size of 1.
This is known as an abbreviation of this which is right here because now I'm just passing in an abbreviated
tuple instead of calling integer slider.
This is an integer slider.
Moreover, if we wanted this to be a floating point slider all you had to do was specify one of these to be
a float here and then if we specify the rest to be floating as well.
So say point 1 and we rerun this.
Now Jupiter which is realized oh he wants a foot slider.
So then we have the floating point slider here.
Moreover, notice how our step size is point 1, and we're going from negative ten points zero all the way to
positive 10.0 and step sizes.
Point 1 again.
I am using this abbreviated format.
Moreover, as always you can do this with the interactive decorator as well.
So if I say at interact, I could say x is equal to a zero point zero to twenty point zero step size of
0.5 create a function let's have this function B H, and we can have some default value like five
point zero and then return x.
So then when I call it, I have the default value of five points zero.
It goes down to zero points zero up to 20 steps 0.5.
OK.
So this is widget abbreviation again lots of different options here.
Those were the abbreviations for integers and floating point sliders.
You can also have dropdown menus, and those are abbreviated as well.
Remember that if I said interact pass and my function and I said x is equal to a string such as Hello.
It just has a text box here automatically generates a hello how are you.
Moreover, it's just returning that string.
If you pass in a list of strings as an abbreviation of a for the call.
It will create a dropdown menu so it can say hello option to a comma the string option 3.
Moreover, now we run this.
We get back this excellent little dropdown menu option to hello.
Option 3 so this abbreviation of using a list of strings is a dropdown menu.
Moreover, we have a little table in the notebook so you can reference this and I have to memorize what tuples
stand for what lists stand for, but when you get used to it, it's pretty straightforward.
These tuples are slider's the list with strings is going to be a dropdown menu.
Moreover, there's also when you pass in a dictionary the Mishu happens will say interact.
Our simple function says X is equal to 1 10 and then we'll say to goes with 20.
Moreover, when you run this you get back adopt them, and you want to, but then the actual output is equal to
the value here.
So your dropdown menu is the key, and your output is the actual value.
So notice the difference here between that list and that dictionary being passed in there.
Now in addition to this interact function, I Python or Jupiter, in general, provides another function called
interactive instead of, and it's useful when you want to reuse the widgets that are already
been produced or access the data that is bound to the user interface controls.
So unlike interact, the return value of the function will not be displayed automatically but you can
display a value inside the function using python that displays.
So you what I mean by all this say from a python that displays import display and let's create a function.
here simple function f takes an A and B, and then instead of just returning a plus b We're also
going to call display on a plus b then and this will allow us to use the interactive function.
So I could say w equal to interactive pass an F again.
Moreover, I will say she is equal to 10 B is equal to 20.
Moreover, if I check what type w is notice it's now an interactive object, so the widget isn't interactive.
which if you look at the documentation is a subclass of something called the box which is just a container
for other widgets.
Now the children of interactive we check out the children attribute.
These are two integer sliders here as well as an output.
So it's building out a more extensive UI for you using the interactive functionality.
So then if you want to finally see what W looks like what it was defined by this interactive.
the function you can call display on w makes her spell that right.
Moreover, then you get back both sliders so you get 27 negative two and you get to see the sumps 27 minutes.
to 25.
Moreover, I can see how we can use the interactive functionality to save something like a variable and then
just display the variable later on.
OK, so that's it for the interact and interactive functionality.
Let's very briefly go over everything we just covered.
The main idea here is that you're going to be able to create functions that take some input.
then return some output.
Then if you want your user to have some interface to change the possible inputs and then see
the outputs you can use the Interact function, or you can use the interactive decorator.
Moreover, then we saw how we could specify actual widgets inside of the Interact call itself.
Moreover, we saw how there are abbreviations for them such as a tuple for a slider or the list for a dropdown.
menu or a dictionary for a dropdown menu that has a connection to some critical value pairs.
Then finally we saw how we could use the interactive functionality to save this to a variable.
So I can later display that variable and again see the inputs and outputs.
The last thing I want to note here is when you use that in the book you may have noticed that there is this
report here that the output is some function.
If you don't want to see that all you have to do is have a semi-colon here and then when you run this
you will no longer see the output.
Moreover, that's what's mostly happening when you call display on the W.
You don't see that little output cell OK.
We'll see you in the next python course, or we begin to discuss more capabilities of widgets.
Advertise on APSense
This advertising space is available.
Post Your Ad Here
Post Your Ad Here
Comments (2)
Manoj Singh Rathore10
Digital Marketer
Techstack®
4th Floor, plot 7, Lane 2, Behind Saket Metro Gate, 2, Westend Marg, Saiyad ul Ajaib, Sainik Farm, New Delhi, Delhi 110030
093198 44494
Manoj Singh Rathore10
Digital Marketer
Techstack
Internet marketing service
4th Floor, plot 7, Lane 2, Behind Saket Metro Gate, 2, Westend Marg, Saiyad ul Ajaib, Sainik Farm, New Delhi, Delhi 110030
techstack.in
093198 44494