Welcome to the Treehouse Community

The Treehouse Community is a meeting place for developers, designers, and programmers of all backgrounds and skill levels to get support. Collaborate here on code errors or bugs that you need feedback on, or asking for an extra set of eyes on your latest project. Join thousands of Treehouse students and alumni in the community today. (Note: Only Treehouse students can comment or ask questions, but non-students are welcome to browse our conversations.)

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and a supportive community. Start your free trial today.

Python Flask Basics Templates and Static Files Flat HTML

Joseph Martin Nograles
Joseph Martin Nograles
4,696 Points

When passing through to html

Hi!

Two questions

Why do we use "num1" = num1 when def add while name = name when def index? Why doesn't "name" = name work?

Chris Freeman
Chris Freeman
Treehouse Moderator 67,636 Points

Can you expand your question with larger code examples?

1 Answer

Myers Carpenter
Myers Carpenter
6,111 Points

So the code in question looks like this:

code.py
def index(name="Treehouse"):
    return render_template("index.html", name=name)

# ...
def add(num1, num2):
   context = {"num1": num1, "num2": num2}
   return render_template("index.html", **context)

To understand what's going on in add() you need to know about keyword args and dicts. Python allows you to pass keyword args to a callable (aka method or function). The most often you will see them like this render_template("index.html", name=name), where name is a keyword arg.

Sometimes you want to make a variable that has all the keyword args you are going to pass to a callable later. That's what's going on in add() with the context var. Kenneth creates a dict, then later expands that dict with **context as keyword arguments to render_template(). If you go back in the video he shows an alternative way of writing it render_template("index.html", num1=num1, num2=num2).

Hope that helps!