I am trying with the below code to redirect to the item page using fast API. I used TemplateResponse to redirect to the index.html page which I already created inside the templates folder.
The file structure is as follows:-
main
app.py
templates -> index.html
static -> style.css
app.py
from fastapi import FastAPI, Request
from fastapi.responses import HTMLResponse
from fastapi.staticfiles import StaticFiles
from fastapi.templating import Jinja2Templates
app = FastAPI()
app.mount("/static", StaticFiles(directory="../static"), name="static")
templates = Jinja2Templates(directory="templates")
#app.get("/items/{id}", response_class=HTMLResponse)
async def read_item(request: Request, id):
return templates.TemplateResponse("index.html", {"request": request, "id": id})
I tried doing it but I am getting an error "jinja2.exceptions.TemplateNotFound: index.html" when I insert some HTML code statically it's working but TemplateResponse is not working properly. Even when I tried giving an entire path of the HTML file inside the templates folder it's giving a different error.
Please guide me on how to make this code work as I tried different ways but in any case, this code is giving an error index.html template not found
Try setting the template path like this:
from pathlib import Path
BASE_DIR = Path(__file__).resolve().parent
templates = Jinja2Templates(directory=str(Path(BASE_DIR, 'templates')))
Related
Got a problem reading an asset in Flutter web app. I've declared it in pubspec.yaml
But when I'm trying to load it with await rootBundle.loadString('test/sample_text.json'); I always get the same error Error while trying to load an asset: Failed to load asset at "assets/test/sample_text.json"
Never had such issue when developing for mobile
Assets in the web are placed under another assets/ directory, which results in the path being assets/assets/....
Create a simple function e.g. in lib/utils.dart:
import 'package:flutter/foundation.dart';
String path(str) {
return (kIsWeb) ? 'assets/$str' : str;
}
Wrap any path strings with this function, for example
AssetImage(path('assets/test/sample_text.json')).
the correct way to do this is packages/$your_package/assets/test/sample_text.json
I created a React App using the create-react-app npm package. Everything works fine, however I'm not sure how to handle external files.
I created a POC that holds a json object of the entries for a select field. I can import the object and it works properly. However, I'm used to having files like this available on the server so that they can be edited without having to re-build the program.
I converted the file to a .json and I'm able to import it without a problem. However, if I try to move it to the "public" folder, I'm no longer able to import it. If it remains within the src folder it gets included within the bundle and I'm not able to edit it directly.
Is including the file within the bundle the standard way of handling data that can change (whether by requiring update or i18n)??? If not, how do I go about configuring the app to allow me to import it from the public folder once it's deployed?
This should work:
Add data.json to public directory and use fetch in componentDidMount
componentDidMount() {
fetch('data.json').then(data => {
data.json().then(response => {
console.log(response)
this.setState({ data }); // you can set data to state
});
})
}
content of data.json
{
"data": "some data"
}
If you are trying to use some external data e.g. from server, you should not import it like any other files (i.e. import a.js from './a') you should make HTTP request in order to retrieve these data
This is the app component. It gives an error that it can't find module pages/question/pages/question1/question1
This is the app module where I declared the question1. still the same error.
Your import path is wrong, it should be as,
import {question1} from './pages/questions/pages/question1'
I'm developing a Pyramid project with jinja2 templating engine. Following the jinja2 documentation I've find out a way to load different templates from a unique view. But taking into account that the module pyramid_jinja2 was already configured in my app with a default path for templates. I was wondering if there is another way more elegant to get this done. This is my approach:
from jinja2 import Environment, PackageLoader
#view_config(context=Test)
def test_view(request):
env = Environment(loader=PackageLoader('project_name', 'templates'))
template = env.get_template('section1/example1.jinja2')
return Response(template.render(data={'a':1,'b':2}))
Can I get an instance of the pyramid_jinja2 environment from somewhere so I don't have to set again the default path for templates in the view?
The following is enough:
from pyramid.renderers import render
template = "section/example1.jinja2"
context = dict(a=1, b=2)
body = render(template, context, request=request)
And to configure loading do in your __init__.py:
config.add_jinja2_search_path('project_name:templates', name='.jinja2', prepend=True)
I'm using Sikuli IDE 1.0.0 on Mac, trying to get a simple test case working where I call a script in one module from another.
The modules are all in the same directory.
testModule.sikuli just has this:
from sikuli import *
def testFunc():
exit(1)
testImport.sikuli just has this:
import testModule
reload(testModule)
testModule.testFunc()
running testImport just yields:
[error] ImportError ( No module named testModule )
on the import testModule line.
I've tried various additions to testImport including:
myScriptPath="[my project path]"
if not myScriptPath in sys.path: sys.path.append(myScriptPath)
None of these seem to work.
I think the import just brings the new functions into the same module.
Try calling testFunc() instead of testModule.testFunc().
I've encountered the same issue. I have solved this problem using classes.
Try this code:
testModule.sikuli:
from sikuli import *
class test:
def testFunc(self):
exit(1)
testImport.sikuli:
import testModule
foo = testModule.test()
foo.testFunc()
This should work assuming your files are in the same folder (for example ./test/testImport.sikuli and ./test/testModule.sikuli)