Forums

Multiple Web Pages for a Single App

What follows is probably a dumb question, but I'm a beginner and a little at a loss:

I'm trying to create a web app that takes inputs from an html form, passes those inputs as variables in a piece of python code, then posts the resulting outputs from the python script back onto the web.

My question is whether it is possible for me to have one page for the inputs ([myname].pythonanywhere.com/home) and a separate page for the results display ([myname].pythonanywhere.com/results) for a single app. Or do I need to figure out a way to show the results on the home page? Do I need to buy space for another app? Am I missing something obvious?

yup. totally possible.

I see that you are using django. In that case you will want to add to urls.py and views.py etc to get a second page (/results) working.

I would recommend going through the django tutorial to learn how it works.

I figured that was the case - found a syntax error in the way I had set up urls.py to accommodate the new page. Thanks for the quick reply!

             / ̄ ̄ ̄ ̄ ̄ ̄ ̄ ̄ ̄\    
<<<<<<:>~  <   Yay!          |   
             \_________/

Also a noob here. How would one accomplish this for a Flask app?

I would suggest working through any Flask tutorial. They will all cover that.

I need to do the same thing (a web app that takes inputs from an html form, passes those inputs as variables in a piece of python code, then posts the resulting outputs from the python script back onto the web). I have it working on my computer (local host) using python -m http.server

I have signed up for pythonanywhere.

I have never used Django or Flask or web2py or Bottle. Which one do you recommend that I use to get my web app up and running here? I would like to use the quickest and easiest. I won't be doing anything beyond this project.

That's very similar to what I'm needing!

In my case I'm needing to receive an incoming http post request from plivo, which needs to go to a specific URL (not entirely sure how similar that is). Which means I need to get it set up on my web app, and currently I'm wrestling with the virtualenv to get that functional. But when that happens, I'll need to be able to take the request, pass it into python functions, and do stuff with it.

Thanks in advance to such wizards who can help us!

We did a blog post a while back about how to turn a Python script into a simple website using Flask -- if you work through the examples there, hopefully all will become clear :-)

Hi I too am struggling with something similar on Flask. I've setup and index.py that has buttons on it that WILL link to HTML pages, however, I can't get those HTML pages to run the python scripts I require them to. So, I remade the html pages into option1.py, option2.py and these pages work perfectly from the wsgi script, however, they cannot be linked using the .py pages from the index page...I can't find help on what I'm missing/doing incorrectly. I've followed the tutorials and they're all based on just linking the wsgi script to 1 page (although do use defined functions in another .py script). Does any of that even make sense? Thanks, Jonny

Hi Jonny -- sorry, I'm a bit confused by the terminology you're using. Are you writing your site as a raw WSGI one? Or are you using a framework like Flask?

..oops

Hi Giles. I am using Flask and have followed 2x Flask tutorials which have helped enormously in creating the web pages and calls to a processing script (which does all the maths for me). So basically the WSGI script runs the flask_app.py, which sets up a comments box/database then does a render_template for my index.html page in my templates folder (which is a home page, with 3x links/bittons to 3x other web pages (each page doing different functions on user inputted numbers). BUT although I can open the html pages, these then won't call any functions/python scripts, so I recreated the same pages as python scripts containing html code. The python pages work if I link from the WSGI, but so not work if I follow a hyperlink to them...so I'm a little stuck at the moment. Do I create hyperlinks to the html templates and take user inputs from them, or do I hyperlink to the .py versions, and if so...how? Sorry if rhis is confusing, I may well be doing it all completely wrong. If you need to view the site it is passworded. Thanks Jonny

You do not link to py files. Py files contain functions that render your templates and return http responses and urls are based on your routing configuration. See an example in https://blog.pythonanywhere.com/121/#a-first-cut-with-dummy-data

Okay, thanks. I have followed that guide and manged to get a webpage with working comments to work. The issue I'm having is - I have 3 buttons that I want to link to 3 pages respectively that perform particular 'calculations' on user inputted numbers. So, I've made the buttons with hyperlinks that do work, they will display the relevant html pages as required. The issue I'm having is, on those pages the iser input and links to the rhe pyrhon scripts I require do not work. That is to say, when a user inputs the data and clicks submit, nothing happens. Thanks for your reply.

Jonny

How do you pass the input to the python code?

Hi

<form method="POST"..etc

I made working pages of these pages that are python script and html (as per the tutorials). I'm now stuck because I don't know how to code the html to send the inputted data to python. I was mistaken earlier; once the user inputs the data and submits, they get FORBIDDEN (403) page. I'm guessing this is because I haven't told the html where to POST to..?

Thanks for replying

Could you show us how the form looks like?

I think I I've been thinking about this all wrong...

I am now realising that, the Flask_app.py is a running code and I'm supposed to call the html files, like functions; where what I've actually been doing is thinking like a website and going through links to different pages (which works until you require user input)...

Am I along the right lines? If so, I think I'm going to have to reread everyrhing and try to get my head round it.

I've basically adapted the tutorial code to suit my need - but clearly there's more to it than that.

I never set out to learn html. I have several python scripts that, although I've made apk files out of to distribute over android, I couldn't send to my iphone friends. So, web app method seemed the way forward...but...well, not that simple eh!

Yes, you're on the right lines. There are view functions in your code that are called when a request comes in.

Yeah I still can't get my head round it.

This seems to be what I have...

Wsgi points to flask_app.py which sets up a database for comments and then points to /templates/index.html (so the comments box shows on index page, along with 3x buttons). Each of those buttons are hyperlinks that point to 3x new pages -- /templates/page1.html and /templates/page2.html (well 2 at the moment but there will be 3 i hope); this works, but its like I've now ditched flask_app.py because the hyperlinks are the full URL and NOT any reference from flask_app.py

I'm just stumped now, I've tried going back to flask_app and adding a new definition for page1.html with return render_template( etc. but it's still performing in the same way

Perhaps working through the flask tutorial will make things clearer: https://flask.palletsprojects.com/en/3.0.x/tutorial/

Yeah I'll work through that one too, thank you

If I may comment, this looks like Flask issues with some background math. I dumped Flask long time ago. Way too complex for simple stuff, routing, etc.

If you are after some ML, have a look at this example: https://mlearning.pythonanywhere.com/

This is not Flask, but Jam.py. Looks way way simpler than Flask, claimed 2 hrs to build from scratch. Just my 2c.

@ipam2 thanks for sharing -- I see there's even a guide how to deploy on PythonAnywhere on their docs.

You are welcome. Their doc is referring to this: https://github.com/pythonanywhere/help_pages/blob/master/articles/Jampy.md

Im using virtual env which is missing from the docs but you'll figure out :)

@ipam2 thanks for the info!