SeanElder
Posts: 5
Joined: Tue Mar 20, 2018 7:32 am
Location: London

Build a web server first project problems!!

Tue Mar 20, 2018 7:47 am

Good morning everyone

So its my first project here and I am having a problem. I have followed the guidelines but when i reached the fifth part of the project which is titled "building a basic flask web application" everything is going well until I reach the part where I am about to run this code:

Code: Select all

from flask import Flask

  app = Flask(__name__)

  @app.route('/')
  def index():
      return 'Hello world'

  if __name__ == '__main__':
      app.run(debug=True, host='0.0.0.0')
Then in the terminal it shows this message:

File "app.py", line 1
from flask import Flask
^
IndentationError: unexpected indent

It's probably something very basic but I'm stuck so if anyone can help me out it would be great.

thanks anyone

:mrgreen:

texy
Forum Moderator
Forum Moderator
Posts: 5160
Joined: Sat Mar 03, 2012 10:59 am
Location: Berkshire, England

Re: Build a web server first project problems!!

Tue Mar 20, 2018 7:51 am

Hi and welcome to the forum.
This is python code which relies on indentation. Note I edited your post to include the CODE tags, and it highlights the indentation which would of otherwise be missed. The error message tells you that there is an unexpected indentation on that line - remove the indentation and that particular error will go away.
Texy
Various male/female 40- and 26-way GPIO header for sale here ( IDEAL FOR YOUR PiZero ):
https://www.raspberrypi.org/forums/viewtopic.php?f=93&t=147682#p971555

SeanElder
Posts: 5
Joined: Tue Mar 20, 2018 7:32 am
Location: London

Re: Build a web server first project problems!!

Tue Mar 20, 2018 9:19 am

Hello you may have to bear with me a little.

I went back into the file already saved in the Python folder and re - entered the code from the web app. I am just copying and pasting is that i think the terminal automatically ran this:

IndentationError: unexpected indent
[email protected]:~/webapp $ from flask import Flask
from: can't read /var/mail/flask
[email protected]:~/webapp $
[email protected]:~/webapp $ app = Flask(__name__)
bash: syntax error near unexpected token `('
[email protected]:~/webapp $
[email protected]:~/webapp $ @app.route('/')
bash: syntax error near unexpected token `'/''
[email protected]:~/webapp $ def index():
bash: syntax error near unexpected token `('
[email protected]:~/webapp $ return 'Hello world'
bash: return: Hello world: numeric argument required
bash: return: can only `return' from a function or sourced script
[email protected]:~/webapp $
[email protected]:~/webapp $ if __name__ == '__main__':
> app.run(debug=True, host='0.0.0.0')


Also sorry about formatting,

What is a code tag too?

thanks

scotty101
Posts: 3738
Joined: Fri Jun 08, 2012 6:03 pm

Re: Build a web server first project problems!!

Tue Mar 20, 2018 9:31 am

You can't just type the python code in to a terminal window. You have to enter it in to a text editor and then run it with python.
On Raspbian there are a number of IDEs (Text editors specifically for developing code) that are compatible with python.
Here are my suggestions in order of what I think will be easiest for you.
1. Thonny
2. IDLE
3. Geany

You can use a purely text based editor like nano, vim, emacs etc but this might be more complicated for a beginner.
Electronic and Computer Engineer
Pi Interests: Home Automation, IOT, Python and Tkinter

SeanElder
Posts: 5
Joined: Tue Mar 20, 2018 7:32 am
Location: London

Re: Build a web server first project problems!!

Tue Mar 20, 2018 10:29 am

OK Thanks. I think I've bitten off more than I can chew. If I start at the easier projects using code will it show me how to use text editors and such in those projects?

thanks

scotty101
Posts: 3738
Joined: Fri Jun 08, 2012 6:03 pm

Re: Build a web server first project problems!!

Tue Mar 20, 2018 10:34 am

Some tutorials may cover how to use a specific IDE but most will just concentrate on how to code and assume basic computer ability.
Electronic and Computer Engineer
Pi Interests: Home Automation, IOT, Python and Tkinter

User avatar
DougieLawson
Posts: 36331
Joined: Sun Jun 16, 2013 11:19 pm
Location: Basingstoke, UK
Contact: Website Twitter

Re: Build a web server first project problems!!

Tue Mar 20, 2018 10:56 am

SeanElder wrote:
Tue Mar 20, 2018 9:19 am

What is a code tag too?
If we have a bit of python like

if code_tags == "used":
print ("Formatting is preserved")
else:
print ("Formatting is lost")

And we post it on the forum as:
[code]
if code_tags == "used":
print ("Formatting is preserved")
else:
print ("Formatting is lost")
[/code]

Then it comes out as

Code: Select all

if code_tags == "used":
  print ("Formatting is preserved")
else: 
  print ("Formatting is lost")


Which can be copy'n'pasted and has all of the left-hand whitespace (which is part of python syntax) preserved.
Note: Having anything humorous in your signature is completely banned on this forum. Wear a tin-foil hat and you'll get a ban.

Any DMs sent on Twitter will be answered next month.

This is a doctor free zone.

SeanElder
Posts: 5
Joined: Tue Mar 20, 2018 7:32 am
Location: London

Re: Build a web server first project problems!!

Tue Mar 20, 2018 2:09 pm

Thanks Everyone, I have stumbled across the curriculum and I am going to work my way through them. I will add an update to this post when I finally get the skills to complete this project.

thanks very much

:mrgreen:

texy
Forum Moderator
Forum Moderator
Posts: 5160
Joined: Sat Mar 03, 2012 10:59 am
Location: Berkshire, England

Re: Build a web server first project problems!!

Tue Mar 20, 2018 2:50 pm

Your classmate might need a helping hand too :
viewtopic.php?f=91&t=208484

:lol:

Texy
Various male/female 40- and 26-way GPIO header for sale here ( IDEAL FOR YOUR PiZero ):
https://www.raspberrypi.org/forums/viewtopic.php?f=93&t=147682#p971555

Return to “Beginners”