2

Im currently working on a project that has the following structure:

ROOT
 -js
  -dist
   -index.html
   -bunch of other files that index.html uses
 -src
  -main.py
 -doc_apps
  -app1
   -index.html
   -other resources that index.html needs
  -app2
   -index.html
   -other resources that index.html needs

Now my flask configuration is:

ROOT_DIR = os.path.dirname(os.path.abspath(__file__))
COMPONENT_FOLDER = os.path.abspath(os.path.join(ROOT_DIR, '../'))
DIST_FOLDER = os.path.join(COMPONENT_FOLDER, 'js\\dist')
DOCUMENTATION_FOLDER = os.path.join(COMPONENT_FOLDER, 'documentation_data')

app = Flask(__name__, static_folder=DIST_FOLDER, template_folder=DIST_FOLDER, static_url_path='')

my_loader = jinja2.ChoiceLoader([
    app.jinja_loader,
    jinja2.FileSystemLoader(DOCUMENTATION_FOLDER),
])
app.jinja_loader = my_loader

@app.route('/')
def index():
    """
    This the main entry to the application

    Returns
    -------
        out
            renders the 'index.html' file
    """
    return render_template('index.html')

@app.route('/get_index_html', methods=['GET'])
def get_index_html():
    index_html_path = request.args.get('path')
    full_root = os.path.join(index_html_path).replace('\\','/')
    return render_template(full_root)

When I start my app and go to the main page im able to render the index.html (the one in the js/dist folder) and everything works great

the problem starts when I try to render one of the inner index.html.

Im able to render the index.html by itself but I get the following errors

127.0.0.1 - - [02/Aug/2018 18:08:43] "GET /_static/css/theme.css HTTP/1.1" 404 -
127.0.0.1 - - [02/Aug/2018 18:08:43] "GET /_static/pygments.css HTTP/1.1" 404 -
127.0.0.1 - - [02/Aug/2018 18:08:43] "GET /_static/js/modernizr.min.js HTTP/1.1" 404 -
127.0.0.1 - - [02/Aug/2018 18:08:43] "GET /_static/jquery.js HTTP/1.1" 404 -
127.0.0.1 - - [02/Aug/2018 18:08:43] "GET /_static/underscore.js HTTP/1.1" 404 -
127.0.0.1 - - [02/Aug/2018 18:08:43] "GET /_static/doctools.js HTTP/1.1" 404 -
127.0.0.1 - - [02/Aug/2018 18:08:43] "GET /_static/js/theme.js HTTP/1.1" 404 -
127.0.0.1 - - [02/Aug/2018 18:08:43] "GET /_static/jquery.js HTTP/1.1" 404 -
127.0.0.1 - - [02/Aug/2018 18:08:43] "GET /_static/underscore.js HTTP/1.1" 404 -
127.0.0.1 - - [02/Aug/2018 18:08:44] "GET /_static/doctools.js HTTP/1.1" 404 -
127.0.0.1 - - [02/Aug/2018 18:08:44] "GET /_static/js/theme.js HTTP/1.1" 404 -

I can tell that the problem is that it cant find the static files that are needed in order to generate this index.html correctly.

How can I tell it that for this specific index.html use some_static_folder? I was wondering if there is something like render(html, static_folder=...)

I would like to mention that each one of the App1/index.html has its own links and those links rely on the inner structure of App1, so how can I configure that when I send App1/index.html it will use some static_folder for everything that will be invoked from that App1/index.html

Some more info, App1/2 can be added also after the server is already running.

thank you very much for your help

  • Possible duplicate of [More than one static path in local Flask instance](https://stackoverflow.com/questions/9513072/more-than-one-static-path-in-local-flask-instance) – Mick_ Aug 02 '18 at 15:23
  • Hi, I looked into it before posting and it didnt seem to help me (or im simply missing something) – Bar Dubovski Aug 02 '18 at 15:40

0 Answers0