How to open html files in python file?

How my file system looks like:

---- folder1
     ---- file.py
---- folder2
     ---- file.html

How could I import file.html into file.py?

Can you create a .txt file from the html file?

https://www.digitalocean.com/community/tutorials/how-to-handle-plain-text-files-in-python-3

If that is a question, the answer is yes. HTML raw source is text. Changing the extension will have no effect on the code, only hide the MIME Type. This is especially unimportant if the HTML is in snippet form and is inserted into the response document.

No, I was asking if she could do that/try that. :slight_smile:

1 Like

Pardon my interruption, then.

No worries at all! :slight_smile:

I’d never done so (outside of using beautiful soup to parse a webpage), so any input is helpful. :slight_smile:

I’m writing a flask program, and I want to use the render_template method to render html files. Problem is, my html files are in another folder, and I really don’t want to put them in the same folder as my program.

Welp, that’s out of my knowledge base. :sweat_smile:

Perhaps someone else can help out?

Is there a way to set up a path to the HTML?

I mean, I’m able to get the absolute path to the file, but I have multiple html files that I will be using, so I just want a way to do something like : from folder_with_html_files import *.

Not sure we can mess with import since it finds all the source modules in the same place. Unfortunately I haven’t taken this course yet so kind of useless to you, at present. Can you give us some context code and perhaps a link to the exercise?

It’s actually not an exercise, it’s a personal project but basically:

LOGIN.HTML
<html>
    <body>            
        <h1>Hello, world!</h1>                    
        Click <a href="/time">here</a> for the time.
    </body>
</html>    
from flask import Flask, request, render_template

app = Flask(__name__)
app.config["DEBUG"] = True

@app.route('/')
def login():
    return render_template('login.html')

app.run()

See that login.html? Except it is in another folder which I can’t access.

Can you not include a directory path in the URI?

For render_template you need a file structure like this:

3 Likes

Does Flask set that up similar to how React is loaded?

I’m very unfamiliar with react’s setup. But basically you set up that structure and render_template knows to look in the template folder.

You do have to have from flask import render_template. But then you can just do whatever xxx.html you have in templates.

So are you saying I need to take my python file out of the folder it is in?

Or your html file. Yes. Flask projects have a particular number of ways to be set up. But they have to follow a particular structure (if you’re going to use some of the tools conventionally).

1 Like

Is this how it should be organized?
image

So, I’m not getting an error anymore, but now I have a image
Error.

The directory for html templates has to be called templates.
You put your css and js in static if you have any.

-templates
|-- index.html
|-- about.html
-static
|-- main.css
|-- script.js
myFlaskApp.py
myData.sqlite

it gets more complicated when you break it down more for models and blueprints, but this skeleton can take you a long way. I recommend Corey Schaffer’s tutoial series on flask. It’s very informative. Miguel Grinberg’s book is also very informative (I’d say even more informative), but a little drier.

3 Likes