1
00:00:05,770 --> 00:00:11,830
Welcome back in this lecture we're going to talk about the debug mode option as we learn about flask.

2
00:00:11,830 --> 00:00:14,240
We're definitely going to make some mistakes along the way.

3
00:00:14,410 --> 00:00:18,830
We can set debug equal to true in our application to help us catch these errors.

4
00:00:19,000 --> 00:00:22,620
Debug Mode also gives us access to a console in the browser.

5
00:00:22,630 --> 00:00:28,950
Let's explore all of the debug options we have by jumping over to our text editor OK here and back in

6
00:00:28,950 --> 00:00:30,180
my text editor.

7
00:00:30,180 --> 00:00:32,310
Right now we just have three views.

8
00:00:32,310 --> 00:00:34,410
There's the home page you for index.

9
00:00:34,450 --> 00:00:39,090
There's this information view and then there's dynamic routeing view where we can take in a puppy name

10
00:00:39,180 --> 00:00:42,520
and display right now in uppercase the puppy's name.

11
00:00:42,550 --> 00:00:48,360
What I'm going to do right now is change this view for puppy and I'm going to have it return sort of

12
00:00:48,360 --> 00:00:55,790
saying uppercase I'm going to have it returned the one hundredth letter of the name.

13
00:00:55,790 --> 00:00:58,750
Keep in mind not every name is going to have 100 letters in it.

14
00:00:58,760 --> 00:01:00,640
In fact most names won't.

15
00:01:00,770 --> 00:01:04,370
And then here we're going to say Index 100.

16
00:01:04,550 --> 00:01:09,320
So what's happening here is I'm basically asking for the index at one hundred and then I'm going to

17
00:01:09,320 --> 00:01:10,480
try to return it.

18
00:01:10,490 --> 00:01:14,090
So right now I'm just going to say APTA run here on line 19.

19
00:01:14,090 --> 00:01:16,040
Let me save my changes.

20
00:01:16,040 --> 00:01:18,300
Now let's run the file.

21
00:01:18,530 --> 00:01:21,860
And let's go to the home page in our browser.

22
00:01:21,890 --> 00:01:22,350
Here we go.

23
00:01:22,370 --> 00:01:27,830
So I'm at the home page in the browser and if I do something like forward slash information it says

24
00:01:27,830 --> 00:01:28,780
Puppies are cute.

25
00:01:28,970 --> 00:01:36,480
Now let's try to visit puppy and I say puppy slash Rufus Now remember Rufus doesn't actually have 100

26
00:01:36,550 --> 00:01:43,090
letters in it which means when I try to do this indexing of name 100 that's going to be an error inside

27
00:01:43,090 --> 00:01:44,310
of this function.

28
00:01:44,350 --> 00:01:49,930
So what Flast does by default is it says you have an internal server error the server encountered internal

29
00:01:49,930 --> 00:01:55,000
error and was unable to complete your request which makes sense that function inside of a web application

30
00:01:55,330 --> 00:01:56,640
has an internal error.

31
00:01:56,710 --> 00:02:01,390
So instead of just breaking down the entire site it just reports back you have an internal server error.

32
00:02:01,480 --> 00:02:02,770
That's not very helpful however.

33
00:02:02,780 --> 00:02:04,900
We're trying to debug our code.

34
00:02:04,900 --> 00:02:09,370
So instead what we can do is debug equal to true inside of our text editor.

35
00:02:09,370 --> 00:02:13,420
So let's go back and do that and come back here.

36
00:02:13,590 --> 00:02:23,490
I will set debug equal to true and then that save Let me restart my server and we're going to come back

37
00:02:23,490 --> 00:02:24,510
here.

38
00:02:24,630 --> 00:02:26,240
Let's check the home page again.

39
00:02:27,130 --> 00:02:28,150
So home page looks good.

40
00:02:28,150 --> 00:02:31,540
It says Hello puppy and what I'm going to say is could be.

41
00:02:31,600 --> 00:02:35,240
And the Rufous again and now notice what happens if I'm going to expand this.

42
00:02:35,470 --> 00:02:36,960
I get an index error.

43
00:02:36,970 --> 00:02:40,810
Now I have a lot more information about what's happening within my application.

44
00:02:40,810 --> 00:02:43,270
It says street index out of range.

45
00:02:43,270 --> 00:02:48,550
So now we're already thinking the idea hey maybe I have an index problem with my actual function which

46
00:02:48,550 --> 00:02:50,210
is exactly what was happening.

47
00:02:50,230 --> 00:02:55,260
We pass that rufous Rufus's I have 100 letters so the index is an issue and it'll give you a full traceback

48
00:02:55,330 --> 00:03:00,460
here and you notice as you go around it's say something like at the very bottom just what triggered

49
00:03:00,460 --> 00:03:04,720
the index string index out of range 100 flooder format name.

50
00:03:04,720 --> 00:03:09,940
Now what's really cool is as you hover over these particular errors with the traceback you'll get little

51
00:03:09,940 --> 00:03:11,140
console icon.

52
00:03:11,170 --> 00:03:16,300
And if you hit this you can open an interactive python shell inside of this very frame.

53
00:03:16,310 --> 00:03:18,260
Now in order to do that.

54
00:03:18,310 --> 00:03:24,340
Going to need to expand this as you scroll up here you should eventually see the full traceback error

55
00:03:24,430 --> 00:03:25,880
in this command line.

56
00:03:26,050 --> 00:03:30,460
And then if you also come back here you should see a debugger pin you're going to need that in order

57
00:03:30,460 --> 00:03:32,500
to actually access the console.

58
00:03:32,500 --> 00:03:37,840
So notice right when you set run Osei debug mode is equal to on will say debuggers active and it has

59
00:03:37,840 --> 00:03:38,950
a debugger pin.

60
00:03:39,190 --> 00:03:44,710
I'm going to highlight this debugger pin right click and then select Copy make sure don't do Control-C

61
00:03:44,980 --> 00:03:46,880
that's going to quit your server if you do that.

62
00:03:47,050 --> 00:03:51,610
But once I have that debugger pin let me go back to my browser here and back on my browser going to

63
00:03:51,610 --> 00:03:52,720
expand this again.

64
00:03:52,870 --> 00:03:58,300
I'm going to select the console and then it's going to ask me hey what was that pen.

65
00:03:58,330 --> 00:03:59,850
I'm going to paste that in.

66
00:03:59,860 --> 00:04:00,070
Click.

67
00:04:00,070 --> 00:04:01,450
Confirm pin.

68
00:04:01,480 --> 00:04:05,900
And then once you confirm it you'll be able to scroll back down here and now it says consolers ready.

69
00:04:05,920 --> 00:04:07,170
So what is this useful for.

70
00:04:07,270 --> 00:04:12,310
Well it's really nice here is I can then pass in whatever variables I want and essentially a debugger

71
00:04:12,310 --> 00:04:15,020
counsel right where the traceback happen.

72
00:04:15,040 --> 00:04:20,950
So if I type in name Ill say hey the string that was passed then was rufous and then if I type in name

73
00:04:21,010 --> 00:04:24,310
of 100 ill say another traceback here.

74
00:04:24,310 --> 00:04:26,960
Hey you have an error index string is out of range.

75
00:04:27,010 --> 00:04:31,150
So here I can explore the various variables that are actually happening inside of this view function

76
00:04:31,510 --> 00:04:34,510
and these are the two main aspects of debug.

77
00:04:34,510 --> 00:04:39,400
The first one is this fool that traceback that it's going to issue you and a lot of times just from

78
00:04:39,400 --> 00:04:44,290
this itself you'll be able to debug that if you need to play around whatever variables that are inside

79
00:04:44,440 --> 00:04:50,710
the view function or really any other page that's going on you're able to open a python console within

80
00:04:50,740 --> 00:04:52,180
a step of this traceback.

81
00:04:52,180 --> 00:04:54,460
So those are the two main things to note here.

82
00:04:54,460 --> 00:04:59,230
Remember you'll need the debugger pin in order to actually access this console which is a nice little

83
00:04:59,230 --> 00:05:01,970
security feature that's built into flask.

84
00:05:02,020 --> 00:05:02,630
One last thing.

85
00:05:02,650 --> 00:05:07,630
Keep in mind is that you should never have debug equals true when you're deploying this to production.

86
00:05:07,630 --> 00:05:13,270
Otherwise other people are going to see this debug page and be able to see your full traceback instead

87
00:05:13,270 --> 00:05:15,540
of just getting that internal server error.

88
00:05:15,550 --> 00:05:16,130
All right.

89
00:05:16,330 --> 00:05:17,750
That's it for the debug mode.

90
00:05:17,920 --> 00:05:22,930
From now on we'll be sending debug eagle to true for most of our projects to help us debug any issues

91
00:05:22,930 --> 00:05:24,250
that may come along the way.

92
00:05:24,340 --> 00:05:28,630
And I would definitely suggest to you as well as you're playing around a flask the debug equals the

93
00:05:28,630 --> 00:05:30,310
true and only set it to False.

94
00:05:30,310 --> 00:05:34,560
Once you're ready for production do you have any questions feel free to post the Q&amp;A forums.

95
00:05:34,600 --> 00:05:35,990
We'll see you in the next lecture.
