Webpack for serving JS per Flask Application - python

I have a Flask application I have built locally and have hit the wall as to having javascript with npm packages being able to be used because I am consuming REST APIs and using jQuery per my application. I followed the advice here and have Webpack working.
I ran the command npm run watch
webpack --progress -d --config webpack.config.js --watch
0% compiling
Webpack is watching the files…
Version: webpack 2.1.0-beta.21
Time: 4836ms
Asset Size Chunks Chunk Names
bundle.js 5.97 MB 0 [emitted] main
[366] ./js/index.js 15.3 kB {0} [built]
+ 366 hidden modules
So now I have webpack working with my index.js file and I have the Flask app being served using gunicorn and Docker on 5000.
Everything is working, but my JS is still unresponsive in my Flask application.
Does anyone know what I do to have my index.js file to work per this Flask app with these components I have, or what I am doing incorrectly here?
Here is my html template, rendering just fine. Just not any Javascript:
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1">
<meta name="description"
content="{% block meta_description %}{% endblock %}">
<title>{% block title %}{% endblock %}</title>
<link
rel="stylesheet"
href="{{ url_for('static', filename='styles/vendor/bootstrap.min.css') }}">
<link
rel="stylesheet"
href="{{ url_for('static', filename='styles/main.css') }}">
<link
rel="stylesheet"
href="{{ url_for('static', filename='styles/style.css') }}">
<link
rel="stylesheet"
href="{{ url_for('static', filename='styles/watson.css') }}">
<link
rel="stylesheet"
href="{{ url_for('static', filename='styles/vendor/font-awesome.min.css') }}">
<script src="https://code.jquery.com/jquery-1.12.2.min.js"></script>
<script type="text/javascript" src="https://cdnjs.cloudflare.com/ajax/libs/jquery/1.12.4/jquery.min.js"></script>
<script type="text/javascript" src="https://cdnjs.cloudflare.com/ajax/libs/jqueryui/1.11.4/jquery-ui.min.js"></script>
<script type="text/javascript" src="https://cdnjs.cloudflare.com/ajax/libs/jquery-cookie/1.4.1/jquery.cookie.min.js"></script>
<script src="//netdna.bootstrapcdn.com/bootstrap/3.1.1/js/bootstrap.min.js"></script>
</head>
<body>
<nav class="navbar navbar-default navbar-fixed-top">
<div class="container">
<div class="navbar-header">
<button type="button" class="navbar-toggle collapsed"
data-toggle="collapse" data-target="#navbar"
aria-expanded="false" aria-controls="navbar">
<span class="sr-only">Toggle navigation</span>
<span class="icon-bar"></span>
<span class="icon-bar"></span>
<span class="icon-bar"></span>
</button>
<a href="{{ url_for('page.home') }}">
</a>
</div>
</div>
</nav>
<main class="container">
<div class="md-margin-top">{% block heading %}{% endblock %}</div>
{% block body %}{% endblock %}
</main>
<footer class="footer text-center">
<div class="container">
<ul class="list-inline">
<li class="text-muted">2018</li>
<li>NMT</li>
<li>How it works</li>
</ul>
</div>
</footer>
<script
src="{{ url_for('static', filename='scripts/vendor/bootstrap.min.js') }}">
</script>
<script src="{{ url_for('static', filename='js/bundle.js') }}"></script>
<script src="{{ url_for('static', filename='node_modules/watson-developer-cloud/v2.js') }}"></script>
<script src="{{ url_for('static', filename='js/index.js') }}"></script>
</body>
</html>
The parts involved in the question: https://jsfiddle.net/cgnws2u3/3/
Error Logs
I updated filepath and now get this per my js files I am trying to link to my project:
Uncaught ReferenceError: require is not defined
at v2-generated.ts:17

Make sure that your Flask project structure contains static folder as per Flask documentation.
According to the relative bundle.js file path in index.html template, your static directory should contain js directory. Make sure you have it there after using webpack and it contains your bundle.js, index.js and the rest of files mentioned in your template.

Related

I am not able to show the css files on flask app getting error?

Hello can someone help me resolve this issue. I tried the possible solutions of stackoverflow, but still I am getting error. I am a beginner in Flask. Removed some middle part of the code as I wasn't able to post it.
I am getting 404 error on few files:
My HTML template code is:
<!DOCTYPE html>
<html lang="en" class="no-js">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>Indus HTML5 Free Responsive Template | Template stock</title>
<link rel="stylesheet" type="text/css" href="{{ url_for('static', filename='/css/bootstrap.min.css') }}" />
<link rel="stylesheet" type="text/css" href="{{ url_for('static', filename='/css/et-lineicon.css') }}" />
<link rel="stylesheet" type="text/css" href="{{ url_for('static', filename='/css/font-awesome.min.css') }}" />
<link rel="stylesheet" type="text/css" href="{{ url_for('static', filename='/css/style.css') }}" />
<link href='http://fonts.googleapis.com/css?family=Roboto:400,500,400italic,500italic,700' rel='stylesheet' type='text/css'>
<!--[if IE]>
<script src="http://html5shiv.googlecode.com/svn/trunk/html5.js"></script>
<![endif]-->
</head>
<body>
<!-- Preloader -->
<div class="animationload">
<div class="loader">
Loading...
</div>
</div>
<!-- End Preloader -->
<!-- =========================
FOOTER
========================== -->
<footer class="footer">
<div class="container text-center">
<div class="row">
<div class="col-md-12">
<ul class="social list-inline">
<li class="social-btn" id="fb"><i class="fa fa-facebook"></i></li>
<li class="social-btn" id="tw"><i class="fa fa-twitter"></i></li>
<li class="social-btn" id="tbl"><i class="fa fa-tumblr"></i></li>
<li class="social-btn" id="pin"><i class="fa fa-pinterest"></i></li>
<li class="social-btn" id="flk"><i class="fa fa-flickr"></i></li>
</ul>
</div>
</div>
<div class="row">
<p class="copyright">Copyright© 2016 <a href="http://templatestock.co">Template Stock</p>
</div>
</div>
</footer>
<!-- JAVASCRIPTS -->
<script src="{{ url_for('static', filename='/js/jquery.min.js') }}"></script>
<script src="{{ url_for('static', filename='/js/jquery.lwtCountdown-1.0.js') }}"></script>
<script src="{{ url_for('static', filename='/js/jquery.stellar.min.js') }}"></script>
<script src="{{ url_for('static', filename='/js/jquery.nicescroll.min.js') }}"></script>
<script src="{{ url_for('static', filename='/js/animated.js') }}"></script>
<script src="{{ url_for('static', filename='/js/main.js') }}"></script>
</body>
</html>
My Directories:
Well you need to place the static folder in the main directory and not inside the template folder.
.\home
.\templates
- index.html
.\static
- css
- img
- fonts
- js

Calling function from other app into main app

I am trying to get navigation bar to work across all apps. But i am not able to call the function from there.
this is my base.html.
<html lang="en">
<head>
<!-- Required meta tags -->
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">
<!-- Bootstrap CSS -->
<link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.5.0/css/bootstrap.min.css" integrity="sha384-9aIt2nRpC12Uk9gS9baDl411NQApFmC26EwAOH8WgZl5MYYxFfc+NcPb1dKGj7Sk" crossorigin="anonymous">
<!-- Main CSS file -->
{% load static %}
<link rel="stylesheet" href="{% static 'main/main.css' %}">
{% block style %} {% endblock %}
<title>{% block title %} {% endblock %}</title>
</head>
<body>
<nav class="navbar navbar-expand-lg navbar-light bg-light">
<a class="navbar-brand" href="#">Chili Pili</a>
<button class="navbar-toggler" type="button" data-toggle="collapse" data-target="#navbarNav" aria-controls="navbarNav" aria-expanded="false" aria-label="Toggle navigation">
<span class="navbar-toggler-icon"></span>
</button>
<div class="collapse navbar-collapse" id="navbarNav">
<ul class="navbar-nav">
<li class="nav-item active">
<a class="nav-link" href="#">Home <span class="sr-only">(current)</span></a>
</li>
<li class="nav-item btn-login">
Login
</li>
</ul>
</div>
</nav>
<div class="main">
{% block content %}
{% endblock %}
</div>
<!-- Optional JavaScript -->
<!-- jQuery first, then Popper.js, then Bootstrap JS -->
<script src="https://code.jquery.com/jquery-3.5.1.slim.min.js" integrity="sha384-DfXdz2htPH0lsSSs5nCTpuj/zy4C+OGpamoFVy38MVBnE+IbbVYUew+OrCXaRkfj" crossorigin="anonymous"></script>
<script src="https://cdn.jsdelivr.net/npm/popper.js#1.16.0/dist/umd/popper.min.js" integrity="sha384-Q6E9RHvbIyZFJoft+2mJbHaEWldlvI9IOYy5n3zV9zzTtmI3UksdQRVvoxMfooAo" crossorigin="anonymous"></script>
<script src="https://stackpath.bootstrapcdn.com/bootstrap/4.5.0/js/bootstrap.min.js" integrity="sha384-OgVRvuATP1z7JjHLkuOU7Xw704+h835Lr+6QL9UvYjZE3Ipu6Tp75j7Bh/kR0JKI" crossorigin="anonymous"></script>
</body>
</html>
I am trying to call the login function which goes to the login page in login app on main page url. Also I want to make the login function works across different apps. Therefore, I am not able to call the function here (in the example code provided). Is there any way I can call the login function here (in the example code provided). So, I don't have to create login page again.
If your base.html say is in an app called pages, and the template path is pages/templates/pages/base.html you can do in all apps
{% extends 'pages/base.html' %}

FLASK BuildError Python

I cannot find what is wrong with my Flask app. I keep getting werkzeug.routing.BuildError: Could not build URL for endpoint 'mark_report'.
Here is /index
<!DOCTYPE html>
<html> <head>
<title>Title</title>
<link rel="stylesheet" href="../static/css/style.css">
<link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.3.1/css/bootstrap.min.css" integrity="sha384-ggOyR0iXCbMQv3Xipma34MD+dH/1fQ784/j6cY/iJTQUOhcWr7x9JvoRxT2MZw1T" crossorigin="anonymous">
<script src="https://code.jquery.com/jquery-3.3.1.slim.min.js" integrity="sha384-q8i/X+965DzO0rT7abK41JStQIAqVgRVzpbzo5smXKp4YfRvH+8abtTE1Pi6jizo" crossorigin="anonymous"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.14.7/umd/popper.min.js" integrity="sha384-UO2eT0CpHqdSJQ6hJty5KVphtPhzWj9WO1clHTMGa3JDZwrnQq4sF86dIHNDz0W1" crossorigin="anonymous"></script>
<script src="https://stackpath.bootstrapcdn.com/bootstrap/4.3.1/js/bootstrap.min.js" integrity="sha384-JjSmVgyd0p3pXB1rRibZUAYoIIy6OrQ6VrjIEaFf/nJGzIxFDsf4x0xIM+B07jRM" crossorigin="anonymous"></script>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.4.0/jquery.min.js"></script>
<script type="text/javascript" src="{{ url_for('static', filename='js/scripts.js') }}"></script>
<link rel="stylesheet" href="{{ url_for('static', filename='css/style.css') }}"> </head> <body>
{% block content %}
{% endblock %} </body> </html>
Here is our main page
{% extends "index.html" %} {% block content %} <div class="container"> <div class="spacer">
</div> <div class="row header-row">
<h1>EMS Suite</h1> </div> <div class="row">
<div class="col-md-5">
<ul class="list-group">
<li class="list-group-item li-custom"><h3>Mark Report</h3></li> </ul>
</div>
<div class="col-md-7">
{% block page_content %}
{% endblock %}
</div> </div> <nav class="navbar navbar-default navbar-fixed-bottom footer">
<div class="container-fluid">
</div> </nav> </div> {% endblock %}
Here is the route, there is an HTML page named mark_report.html in our template folders.
#app.route('/mark_report')
def mark_report():
return render_template('mark_report.html')
I made the mistake of having the below if statement above my routes.... Thank you everyone
if __name__ == "__main__":
app.run(host='0.0.0.0', port=8080)

Implementing 404 page in Django, but receiving error

I'm trying to implement my 404.html page into my django project, and coming across this error. Everything else is complete in my project, so I'm eager to get this last piece done! any help is greatly appreciated. included the 404.html and head_css.html file. please let me know if you need to see anything else.
Can anyone help with this?
My 404.html code:
<!doctype html>
<html lang="">
<head>
<meta charset="utf-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="description" content="404 Page not found">
<meta content="width=device-width, initial-scale=1, user-scalable=no" name="viewport">
<title>Codes For Anyone</title>
<link rel="shortcut icon" type="image/x-icon" href="{% static 'themes/images/favicon.ico' %}">
<!-- Google icon -->
<link href="https://fonts.googleapis.com/icon?family=Material+Icons" rel="stylesheet">
{% include 'landing/head_css.html' %}
</head>
<body class="body-custom body-404page">
<div class="errorpage">
<div class="wrapper">
<div class="container">
<div class="content-primary">
<h1 class="title">Page Not Found</h1>
<p class="description">The page you are looking for was moved, removed, <br>
renamed or might never existed.</p>
<div class="section-footer">
Back To Homepage
Report Error
</div>
</div><!-- content-primary -->
</div><!-- container -->
</div>
</div>
<!-- chitika ad code -->
<div class=" container-fluid full-width-container contact">
<script type="text/javascript">
( function() {
if (window.CHITIKA === undefined) { window.CHITIKA = { 'units' : [] }; };
var unit = {"calltype":"async[2]","publisher":"andrewdiemer","width":550,"height":250,"sid":"Chitika Default"};
var placement_id = window.CHITIKA.units.length;
window.CHITIKA.units.push(unit);
document.write('<div id="chitikaAdBlock-' + placement_id + '"></div>');
}());
</script>
<script type="text/javascript" src="//cdn.chitika.net/getads.js" async></script>
</div>
<!-- end ad code -->
</div>
<!-- Scripts Starts -->
{% include 'landing/javascript.html' %}
<script>
$(document).ready(function() {
var sPath=window.location.pathname;
var sPage = sPath.substring(sPath.lastIndexOf('/') + 1);
$(".pmd-sidebar-nav").each(function(){
$(this).find("a[href='"+sPage+"']").parents(".dropdown").addClass("open");
$(this).find("a[href='"+sPage+"']").parents(".dropdown").find('.dropdown-menu').css("display", "block");
$(this).find("a[href='"+sPage+"']").parents(".dropdown").find('a.dropdown-toggle').addClass("active");
$(this).find("a[href='"+sPage+"']").addClass("active");
});
});
</script>
<!-- Scripts Ends -->
</body>
</html>
head_css.html:
{% load staticfiles %}
<!-- Bootstrap css -->
<link rel="stylesheet" type="text/css" href="{% static 'assets/css/bootstrap.min.css' %}">
<!-- Propeller css -->
<!-- build:[href] assets/css/ -->
<link rel="stylesheet" type="text/css" href="{% static 'assets/css/propeller.min.css' %}">
<!-- /build -->
<!-- Propeller date time picker css-->
<link rel="stylesheet" type="text/css" href="{% static 'components/datetimepicker/css/bootstrap-datetimepicker.css' %}" />
<link rel="stylesheet" type="text/css" href="{% static 'components/datetimepicker/css/pmd-datetimepicker.css' %}" />
<!-- Propeller theme css-->
<link rel="stylesheet" type="text/css" href="{% static 'themes/css/propeller-theme.css' %}" />
<!-- Propeller admin theme css-->
<link rel="stylesheet" type="text/css" href="{% static 'themes/css/propeller-admin.css' %}">

Django template {% include not working

I'm new to Django. I'm having problem including a sub-template in a main template. The directory structure of my project is attached in the snapshot. I removed the default views.py and created my own folder named "views" and put my views in it. Here is what I've done:
1. app/views/__init__.py
from .home import *
2. app/views/home.py
from django.shortcuts import render
def index(request):
return render(request, 'home.html')
3. app/templates/home.html
{% load staticfiles %}
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="content-type" content="text/html; charset=UTF-8">
<title>
Administrator Section
</title>
<link type="text/css" rel="stylesheet" href="{% static "css/common.css" %}"/>
<link type="text/css" rel="stylesheet" href="{% static "css/theme/transdmin.css" %}"/>
<link type="text/css" rel="stylesheet" href="{% static "css/login.css" %}"/>
<script type="text/javascript" src="{% static "js/jquery-1.10.2.js" %}"></script>
<script type="text/javascript" src="{% static "js/common.js" %}"></script>
<script type="text/javascript" src="{% static "js/login.js" %}"></script>
</head>
<body>
<div>
<div id="wrapper" class="ys-adminform">
{% include "includes.header_logo.html" %}
<form id="form-login" class="admin-section-form frm">
<div class="header">
<br/>
<h1>Login</h1>
<br/>
</div>
<div class="content">
<div class="form-row">
<input name="email" class="input" placeholder="Email" type="text"/>
<div class="user-icon"></div>
</div>
<div class="form-row">
<input name="password" class="input password" placeholder="Password" type="password"/>
<div class="pass-icon"></div>
</div>
</div>
<div class="footerlogin">
<input class="button" name="btn-login" value="Authenticate" type="button"/>
<div class="message" style="font-weight: bold; padding-top:16px;"> </div>
</div>
</form>
</div>
</div>
{% include "includes.footer.html" %}
</body>
</html>
The problem is include is not adding content from the sub-views.
I understand it could be a path problem but I tried various options like:
{% include "includes.header_logo.html" %}
{% include includes.header_logo.html %}
{% include "includes/header_logo.html" %}
{% include "templates.includes.header_logo.html" %}
{% include "app.templates.includes.header_logo.html" %}
etc.
You have to use directory slashes (/), not dots (.)
For example:
{% include "includes.footer.html" %}
becomes this:
{% include "includes/footer.html" %}
As suggested by Leistungsabfall, "includes/footer.html" works. The header was not working because it had:
This line must be crashing and because of which header was not working.
Simply add template folder with slash .... like
{% include "blog/footer.html" %}

Categories