variable routes values get in static files path - html

I was working on web app using cs50 ide but when i changed to my machine everything worked except for variable-routes their values get in the start of static files
I am using Flask and VS Code and it was working well on the ide and I made sure to copy everything and made sure I copied correctly
This is one of my routes but this route adds the word "posts" to the static file path
#app.route("/posts/<post_id>", methods=["GET", "POST"])
#login_required
def open_post(post_id):
if request.method == "GET":
.....

Related

Can we run appium test scripts through flask server on locally connected device

I am trying to run my appium scripts on button click in a html template, but unfortunately I have searched everywhere but still I found no solution.
My html template is inside a templates folder in the flask project directory and I am taking path as an input and want to pass that string to my Appium script and as soon as I click on the Submit button on my template it should launch the test script on my locally connected phone taking the path as a parameter.
Any kind of help will be appreciated.
Regards
I tried adding functions of my test script in the flask route but I have multiple functions in my python script and for every function I would have to create multiple routes. I was expecting to run the python script all at once on the default flask route ('/').
Following is my code for flask_server.py file as per now I am just getting the parameter in it and showing it in the next route but instead I want to pass my appium script here and run it on device on submit.
from flask import Flask
from flask import render_template
from flask import request
from Instagram.instagram_android_automation import
InstagramAndroidAutomation
from flask import Flask, redirect, url_for, request
app = Flask(__name__)
#app.route('/dashboard/<name>')
def dashboard(name):
return 'welcome %s' % name
#app.route('/login',methods = ['POST', 'GET'])
def login():
if request.method == 'POST':
user = request.form['name']
return redirect(url_for('dashboard',name = user))
else:
user = request.args.get('name')
return render_template('login.html')
if __name__ == '__main__':
app.run(debug = True)

Rails Web App. Help display OpenWeather API icons or json on home page

I'm trying to simply make a third party api call to Open Weather and display the result on the home page of my web app. I am able to successfully make the request in my console by running the file but I cannot get it to show up on my view file. Oddly enough I cannot find anything about actually displaying the returned json in ruby on rails.
<h1 class="hello"><%= #weather.posts %></h1>
The index.html (home)
require 'rubygems'
require 'httparty'
class Weather
include HTTParty
base_uri 'http://https://api.openweathermap.org/'
def posts
self.class.get('https://api.openweathermap.org/data/2.5/weather?q=Memphis&appid=36197f2c3289996e0f0fd7a5ef7d851c')
end
end
#weather = Weather.new
puts #weather.posts
The .rb model
class WeatherController < ActionController::Base
response = HTTParty.get('http://https://api.openweathermap.org/data/2.5/weather?q=Memphis&appid=36197f2c3289996e0f0fd7a5ef7d851c')
JSON.parse response, symbolize_names: true
end
The Controller
So when I start up the server I basically get a blank where the weather icon or json should be. No errors, just blank. Considering how little I could find, it seems this might be helpful to others if answered clearly. Thank You
The code in your controller does not look correct. I guess you want to have something like:
class WeatherController < ActionController::Base
def index
#weather = Weather.new
end
end

Dump pickle file and get download without saving it anywhere in Flask App

#app.route('/', methods=['POST'])
def upload_file():
if request.method == 'POST':
if 'files[]' not in request.files:
flash('No file part')
return redirect(request.url)
files = request.files.getlist('files[]')
...Processing my multiple uploaded files in Flask App...
...
b64_encoded_list = base64.b64encode(json_encoded_list)
print(b64_encoded_list)
## b64_encoded_list is a simple string ##
buffer = BytesIO()
buffer.write(b64_encoded_list)
buffer.seek(0)
flash('File(s) successfully uploaded')
return send_file(buffer, mimetype="image/jpg", attachment_filename="license.pem", as_attachment=True)
Now I am able to send and download at web app part but .pem file is corrupted as b64_encoded_list contains b64 encoding of a string stored in .pem files. But after downloading the file from web app, and again reading it back giving me a error - encrypted = pickle.load(open(pemfile, "rb"))
Error - UnpicklingError: invalid load key, 'W'.
How to resolve this I have also used mimetype - application/x-x509-ca-cert, but still same error.
Please help on this!!
Thanks in advance!!
Instead of writing the output to a file, you need to write it to a file-like object ( eg a in-memory file ).
You can use io.BytesIO.
https://docs.python.org/3/library/io.html#io.BytesIO
Afterwards, you send it to the Client.

My first flask app - how to pass html input to python without a database? [duplicate]

This question already has answers here:
Get the data received in a Flask request
(23 answers)
Closed 2 years ago.
I created an app that moves the largest files from the chosen folder to a new folder for inspection. I tried to create an interface for it using django and when I asked my friend, who is fluent in python about the architecture, he said I don't really need a database, so I should rather try flask.
In a flask tutorial, I found the guy is actually using SQLAlchemy and is creating a database. How can I pass HTML input to my python omitting database?
The pure python app I have already created works just fine without any database.
HTML input section:
{% block body %}
<h1>Your computer can run faster today</h1>
<p>You just need to remove the largest files from some of your folders. It's easy with this small project. Enjoy :)</p>
<form>
<label for="fname">Path to the folder you want to remove largest files from:</label><br>
<input type="text" id="fname" name="fname"><br>
<label for="lname">Path to the folder you want to put your removed files for inspection</label><br>
<input type="text" id="lname" name="lname">
</form>
{% endblock %}
Python input section:
print("Type a path to the folder you want to remove the largest files from")
path1 = os.path.abspath(input())
print("Type a path to create a folder for the largest files to inspect them before removing")
path2 = os.path.abspath(input())
path3 = path2 + '/ToBeRemoved'
The most basic Flask app to achieve this will look something like this.
from flask import Flask, request, render_template
app = Flask(__name__)
#app.route('/')
def remove_large_files():
if request.method == 'POST':
path_from = request.form.get('fname')
path_to = request.form.get('lname')
if path_from and path_to:
# process your files
# return a response
return render_template('template.html')
Replace template.html by the template that contains your form.
Refer to the Request object docs for more info.

Is it possible to add service redirect in development mode in Angular 6

I'm working on an App in Angular 6. When the app is deployed, it exchanges data with an online database. When I'm working on the app, however, I would like it to exchange data with a local database. I have a single service setup to do all of the communication, so when I am working on the app I can simply change the base URL, but I was wondering if there was a way I could just redirect that service in development, and then have it go to the database as normal in the production environment.
I am aware I can add a proxy.conf.json file, which I'm already doing to access a separate online API, so I dont know if it is as simple as just adding another element to that json file.
I haven't posted any sample code as this is more of a general question as to methodology in angular versus a specific line of code question, but I'm happy to post whatever people would like to see for clarification.
You can do one thing,
you can configure your url in both environment.ts and environment.prod.ts like
environment = {
...
url: 'something.com/api',
...
}
and use it like
import { environment } from 'environment/environment';
const url = environment.url;
this will give you different url for normal build (Development env.) and prod build (Deploy env.)
Also, don't worry about using just environmet.ts in import statement, as when you create prod build your environment.ts gets content from environment.prod.ts. so you will be using url from environment.prod.ts
In Angular you have access to a function isDevMode() which determines if the app is currently in devmode.
Based on that, you should be able to adjust your base url as needed.
import { isDevMode } from '#angular/core';
private baseUrl: string = '';
const devUrl: string = '...';
const prodUrl: string = '...';
if (isDevMode()) {
this.baseUrl = devUrl
}
else {
this.baseUrl = prodUrl
};