Forums

Super n00b question!!

I am n00b to python frameworks. I would prefer not to use them yet.

I created a webapp with manual configuration, opened wsgi.py file, and I know how to change "content" variable to control "html" code on the page, but... how do I run another python script to render my page????

For example, let's say I have a file index.py in my home directory where I got just simple:

print("Hello world!")

How do I get this specific "index.py" file to run when I access my website? I just want to see

Hello world!

Also, if I do this in my wsgi.py file:

content = """print("Hello world!")"""

I get:

print("Hello world!")

But I want:

Hello world!

Thank you!!!!

Great, welcome to the Python community!

If I understand your question correctly, in your web app you want to run a Python code from a separate file. There is a difference between running a code and reading a file. When you read a file, you get it's contents; when you run a code you get the result of the code execution. To run a Python code you should give your web app access to the code. It's done by the import statement. In your particular scenario it could look like this:

from index import hello

-- assuming you have an index.py in your web app's working directory and a function hello defined in this file. This function should return a string containing contents of your page, e.g. "Hello!" (BTW returning is not the same as printing).

def hello():
    return "Hello!"

Then you assign what this function returns to the content variable in your wsgi file by calling it:

content = hello()

After reloading the web app you should see "Hello!" on your index page.

Pafk, I appreciate the great and thorough response, but I do have one more question for you, please....

What if I want to keep my web files in separate files and I want to be able to render html pages with links to other files. For example, index.py will have a link to a test.py file like this:

print("<a href=test.py>Test</a>")

In other words, I would like the www.website.com/test.py URL to render HTML from python code in test.py, for example:

print("Hello World!")

What do I need to do for this webserver (nginx?) to render .py files with python code in them just by calling them with a url without relying much on wsgi.py? I'm sure I'm missing something very simple here, but I just can't get in! :)

That's not going to work on PythonAnywhere. What you're describing is basically CGI, which is an old way of creating dynamic web pages. The world has moved on from CGI because it has serious issues with security, with resource use and with speed.

Ok. What is the simplest evolutionary step I should learn about if I'm stuck on CGI? Is it WSGI? Is it some basic framework? Any recommended topics to learn?

Unfair question, but still, do you know of any other python scripting environments like PythonAnywhere? I'm basically looking to teach my 8 year old how to create and grow your own HTML + Python website but using only school's Chromebook. (Otherwise, they are learning garbage in schools these days)

And another question. Getting into Python frameworks can be difficult because there are so many, which one do you recommend start with just based on the most simple solution?

Update: I think I'll start with Flask

Flask is a good first place to start.